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 | #ifndef UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_ |
| 18 | #define UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_ |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 26 | #include <android-base/unique_fd.h> |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 27 | #include <base/time/time.h> |
| 28 | |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 29 | #include "update_engine/aosp/apex_handler_interface.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 30 | #include "update_engine/aosp/service_delegate_android_interface.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 31 | #include "update_engine/client_library/include/update_engine/update_status.h" |
| 32 | #include "update_engine/common/action_processor.h" |
| 33 | #include "update_engine/common/boot_control_interface.h" |
Kelvin Zhang | c7a1d1f | 2022-07-29 13:36:29 -0700 | [diff] [blame] | 34 | #include "update_engine/common/clock_interface.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 35 | #include "update_engine/common/daemon_state_interface.h" |
| 36 | #include "update_engine/common/download_action.h" |
Kelvin Zhang | f844198 | 2022-12-07 18:18:47 -0800 | [diff] [blame] | 37 | #include "update_engine/common/error_code.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 38 | #include "update_engine/common/hardware_interface.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 39 | #include "update_engine/common/metrics_reporter_interface.h" |
| 40 | #include "update_engine/common/network_selector_interface.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 41 | #include "update_engine/common/prefs_interface.h" |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 42 | #include "update_engine/payload_consumer/filesystem_verifier_action.h" |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 43 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 44 | |
| 45 | namespace chromeos_update_engine { |
| 46 | |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 47 | enum class OTAResult { |
| 48 | NOT_ATTEMPTED, |
| 49 | ROLLED_BACK, |
| 50 | UPDATED_NEED_REBOOT, |
| 51 | OTA_SUCCESSFUL, |
| 52 | }; |
| 53 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 54 | class UpdateAttempterAndroid |
| 55 | : public ServiceDelegateAndroidInterface, |
| 56 | public ActionProcessorDelegate, |
| 57 | public DownloadActionDelegate, |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 58 | public FilesystemVerifyDelegate, |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 59 | public PostinstallRunnerAction::DelegateInterface, |
| 60 | public CleanupPreviousUpdateActionDelegateInterface { |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 61 | public: |
| 62 | using UpdateStatus = update_engine::UpdateStatus; |
| 63 | |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 64 | UpdateAttempterAndroid(DaemonStateInterface* daemon_state, |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 65 | PrefsInterface* prefs, |
| 66 | BootControlInterface* boot_control_, |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 67 | HardwareInterface* hardware_, |
| 68 | std::unique_ptr<ApexHandlerInterface> apex_handler); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 69 | ~UpdateAttempterAndroid() override; |
| 70 | |
| 71 | // Further initialization to be done post construction. |
| 72 | void Init(); |
| 73 | |
| 74 | // ServiceDelegateAndroidInterface overrides. |
| 75 | bool ApplyPayload(const std::string& payload_url, |
| 76 | int64_t payload_offset, |
| 77 | int64_t payload_size, |
| 78 | const std::vector<std::string>& key_value_pair_headers, |
| 79 | brillo::ErrorPtr* error) override; |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 80 | bool ApplyPayload(int fd, |
| 81 | int64_t payload_offset, |
| 82 | int64_t payload_size, |
| 83 | const std::vector<std::string>& key_value_pair_headers, |
| 84 | brillo::ErrorPtr* error) override; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 85 | bool SuspendUpdate(brillo::ErrorPtr* error) override; |
| 86 | bool ResumeUpdate(brillo::ErrorPtr* error) override; |
| 87 | bool CancelUpdate(brillo::ErrorPtr* error) override; |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 88 | bool ResetStatus(brillo::ErrorPtr* error) override; |
Sen Jiang | 28d8ed9 | 2018-02-01 13:46:39 -0800 | [diff] [blame] | 89 | bool VerifyPayloadApplicable(const std::string& metadata_filename, |
| 90 | brillo::ErrorPtr* error) override; |
Yifan Hong | 6f7e29f | 2019-12-13 14:41:06 -0800 | [diff] [blame] | 91 | uint64_t AllocateSpaceForPayload( |
| 92 | const std::string& metadata_filename, |
| 93 | const std::vector<std::string>& key_value_pair_headers, |
| 94 | brillo::ErrorPtr* error) override; |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 95 | void CleanupSuccessfulUpdate( |
| 96 | std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface> callback, |
| 97 | brillo::ErrorPtr* error) override; |
Tianjie | 7f8f2ab | 2021-07-23 17:08:50 -0700 | [diff] [blame] | 98 | bool setShouldSwitchSlotOnReboot(const std::string& metadata_filename, |
| 99 | brillo::ErrorPtr* error) override; |
| 100 | bool resetShouldSwitchSlotOnReboot(brillo::ErrorPtr* error) override; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 101 | |
| 102 | // ActionProcessorDelegate methods: |
| 103 | void ProcessingDone(const ActionProcessor* processor, |
| 104 | ErrorCode code) override; |
| 105 | void ProcessingStopped(const ActionProcessor* processor) override; |
| 106 | void ActionCompleted(ActionProcessor* processor, |
| 107 | AbstractAction* action, |
| 108 | ErrorCode code) override; |
| 109 | |
| 110 | // DownloadActionDelegate overrides. |
| 111 | void BytesReceived(uint64_t bytes_progressed, |
| 112 | uint64_t bytes_received, |
| 113 | uint64_t total) override; |
| 114 | bool ShouldCancel(ErrorCode* cancel_reason) override; |
| 115 | void DownloadComplete() override; |
| 116 | |
Kelvin Zhang | 70eef23 | 2020-06-12 20:32:40 +0000 | [diff] [blame] | 117 | // FilesystemVerifyDelegate overrides |
| 118 | void OnVerifyProgressUpdate(double progress) override; |
| 119 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 120 | // PostinstallRunnerAction::DelegateInterface |
| 121 | void ProgressUpdate(double progress) override; |
| 122 | |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 123 | // CleanupPreviousUpdateActionDelegateInterface |
| 124 | void OnCleanupProgressUpdate(double progress) override; |
| 125 | |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 126 | // Check the result of an OTA update. Intended to be called after reboot, this |
| 127 | // will use prefs on disk to determine if OTA was installed, or rolledback. |
| 128 | [[nodiscard]] OTAResult GetOTAUpdateResult() const; |
| 129 | // Intended to be called: |
| 130 | // 1. When system rebooted and slot switch is attempted |
| 131 | // 2. When a new update is started |
| 132 | // 3. When user called |ResetStatus()| |
| 133 | bool ClearUpdateCompletedMarker(); |
| 134 | |
Kelvin Zhang | 1304fe7 | 2021-10-06 19:12:12 -0700 | [diff] [blame] | 135 | void set_update_certificates_path( |
| 136 | const std::string& update_certificates_path) { |
| 137 | update_certificates_path_ = update_certificates_path; |
| 138 | } |
| 139 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 140 | private: |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 141 | friend class UpdateAttempterAndroidTest; |
| 142 | |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 143 | // Return |true| only if slot switched successfully after an OTA reboot. |
| 144 | // This will return |false| if an downgrade OTA is applied. Because after a |
| 145 | // downgrade OTA, we wipe /data, and there's no way for update_engine to |
| 146 | // "remember" that a downgrade OTA took place. |
| 147 | [[nodiscard]] bool OTARebootSucceeded() const; |
| 148 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 149 | // Schedules an event loop callback to start the action processor. This is |
| 150 | // scheduled asynchronously to unblock the event loop. |
| 151 | void ScheduleProcessingStart(); |
| 152 | |
| 153 | // Notifies an update request completed with the given error |code| to all |
| 154 | // observers. |
| 155 | void TerminateUpdateAndNotify(ErrorCode error_code); |
| 156 | |
| 157 | // Sets the status to the given |status| and notifies a status update to |
| 158 | // all observers. |
| 159 | void SetStatusAndNotify(UpdateStatus status); |
| 160 | |
| 161 | // Helper method to construct the sequence of actions to be performed for |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 162 | // applying an update using a given HttpFetcher. The ownership of |fetcher| is |
| 163 | // passed to this function. |
| 164 | void BuildUpdateActions(HttpFetcher* fetcher); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 165 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 166 | // Writes to the processing completed marker. Does nothing if |
| 167 | // |update_completed_marker_| is empty. |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 168 | [[nodiscard]] bool WriteUpdateCompletedMarker(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 169 | |
| 170 | // Returns whether an update was completed in the current boot. |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 171 | [[nodiscard]] bool UpdateCompletedOnThisBoot(); |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 172 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 173 | // Prefs to use for metrics report |
| 174 | // |kPrefsPayloadAttemptNumber|: number of update attempts for the current |
| 175 | // payload_id. |
| 176 | // |KprefsNumReboots|: number of reboots when applying the current update. |
| 177 | // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update. |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 178 | // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the |
| 179 | // current update. |
| 180 | // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of |
| 181 | // the current update. |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 182 | // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current |
| 183 | // payload_id. |
| 184 | // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since |
| 185 | // the last successful update. |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 186 | |
| 187 | // Metrics report function to call: |
| 188 | // |ReportUpdateAttemptMetrics| |
| 189 | // |ReportSuccessfulUpdateMetrics| |
| 190 | // Prefs to update: |
| 191 | // |kPrefsSystemUpdatedMarker| |
| 192 | void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code); |
| 193 | |
Kelvin Zhang | 4061c51 | 2021-05-25 13:42:55 -0400 | [diff] [blame] | 194 | // This function is called after update_engine is started after device |
| 195 | // reboots. If update_engine is restarted w/o device reboot, this function |
| 196 | // would not be called. |
| 197 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 198 | // Metrics report function to call: |
| 199 | // |ReportAbnormallyTerminatedUpdateAttemptMetrics| |
| 200 | // |ReportTimeToRebootMetrics| |
| 201 | // Prefs to update: |
| 202 | // |kPrefsBootId|, |kPrefsPreviousVersion| |
Kelvin Zhang | a43d6e8 | 2021-05-26 10:14:42 -0400 | [diff] [blame] | 203 | void UpdateStateAfterReboot(OTAResult result); |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 204 | |
| 205 | // Prefs to update: |
Tianjie Xu | 2a0ea63 | 2018-08-06 12:59:23 -0700 | [diff] [blame] | 206 | // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|, |
| 207 | // |kPrefsUpdateBootTimestampStart| |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 208 | void UpdatePrefsOnUpdateStart(bool is_resume); |
| 209 | |
| 210 | // Prefs to delete: |
xunchang | 9cf5262 | 2019-01-25 11:04:58 -0800 | [diff] [blame] | 211 | // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded| |
Tianjie Xu | d4777a1 | 2017-10-24 14:54:18 -0700 | [diff] [blame] | 212 | // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|, |
xunchang | 9cf5262 | 2019-01-25 11:04:58 -0800 | [diff] [blame] | 213 | // |kPrefsUpdateBootTimestampStart| |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 214 | void ClearMetricsPrefs(); |
| 215 | |
Yifan Hong | bd47d62 | 2019-12-13 14:59:58 -0800 | [diff] [blame] | 216 | // Return source and target slots for update. |
| 217 | BootControlInterface::Slot GetCurrentSlot() const; |
| 218 | BootControlInterface::Slot GetTargetSlot() const; |
| 219 | |
| 220 | // Helper of public VerifyPayloadApplicable. Return the parsed manifest in |
| 221 | // |manifest|. |
| 222 | static bool VerifyPayloadParseManifest(const std::string& metadata_filename, |
| 223 | DeltaArchiveManifest* manifest, |
| 224 | brillo::ErrorPtr* error); |
| 225 | |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 226 | // Enqueue and run a CleanupPreviousUpdateAction. |
| 227 | void ScheduleCleanupPreviousUpdate(); |
| 228 | |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 229 | // Notify and clear |cleanup_previous_update_callbacks_|. |
| 230 | void NotifyCleanupPreviousUpdateCallbacksAndClear(); |
| 231 | |
| 232 | // Remove |callback| from |cleanup_previous_update_callbacks_|. |
| 233 | void RemoveCleanupPreviousUpdateCallback( |
| 234 | CleanupSuccessfulUpdateCallbackInterface* callback); |
| 235 | |
Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 236 | DaemonStateInterface* daemon_state_; |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 237 | |
| 238 | // DaemonStateAndroid pointers. |
| 239 | PrefsInterface* prefs_; |
| 240 | BootControlInterface* boot_control_; |
| 241 | HardwareInterface* hardware_; |
| 242 | |
Mohammad Samiul Islam | 24a8279 | 2021-02-12 16:52:36 +0000 | [diff] [blame] | 243 | std::unique_ptr<ApexHandlerInterface> apex_handler_android_; |
| 244 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 245 | // Last status notification timestamp used for throttling. Use monotonic |
| 246 | // TimeTicks to ensure that notifications are sent even if the system clock is |
| 247 | // set back in the middle of an update. |
| 248 | base::TimeTicks last_notify_time_; |
| 249 | |
Amin Hassani | 667cf7b | 2018-07-25 14:32:00 -0700 | [diff] [blame] | 250 | // The processor for running Actions. |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 251 | std::unique_ptr<ActionProcessor> processor_; |
| 252 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 253 | // The InstallPlan used during the ongoing update. |
| 254 | InstallPlan install_plan_; |
| 255 | |
| 256 | // For status: |
| 257 | UpdateStatus status_{UpdateStatus::IDLE}; |
| 258 | double download_progress_{0.0}; |
| 259 | |
Alex Deymo | 0fd51ff | 2016-02-03 14:22:43 -0800 | [diff] [blame] | 260 | // The offset in the payload file where the CrAU part starts. |
| 261 | int64_t base_offset_{0}; |
| 262 | |
Alex Deymo | 87792ea | 2016-07-25 15:40:36 -0700 | [diff] [blame] | 263 | // Helper class to select the network to use during the update. |
| 264 | std::unique_ptr<NetworkSelectorInterface> network_selector_; |
| 265 | |
Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 266 | std::unique_ptr<ClockInterface> clock_; |
| 267 | |
Tianjie Xu | 1b66114 | 2017-09-28 14:03:42 -0700 | [diff] [blame] | 268 | std::unique_ptr<MetricsReporterInterface> metrics_reporter_; |
| 269 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 270 | ::android::base::unique_fd payload_fd_; |
| 271 | |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 272 | std::vector<std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface>> |
| 273 | cleanup_previous_update_callbacks_; |
| 274 | // Result of previous CleanupPreviousUpdateAction. Nullopt If |
| 275 | // CleanupPreviousUpdateAction has not been executed. |
| 276 | std::optional<ErrorCode> cleanup_previous_update_code_{std::nullopt}; |
| 277 | |
Kelvin Zhang | 1304fe7 | 2021-10-06 19:12:12 -0700 | [diff] [blame] | 278 | // The path to the zip file with X509 certificates. |
| 279 | std::string update_certificates_path_{constants::kUpdateCertificatesPath}; |
Kelvin Zhang | f844198 | 2022-12-07 18:18:47 -0800 | [diff] [blame] | 280 | ErrorCode last_error_{ErrorCode::kSuccess}; |
Kelvin Zhang | 1304fe7 | 2021-10-06 19:12:12 -0700 | [diff] [blame] | 281 | |
Alex Deymo | 5e3ea27 | 2016-01-28 13:42:23 -0800 | [diff] [blame] | 282 | DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid); |
| 283 | }; |
| 284 | |
| 285 | } // namespace chromeos_update_engine |
| 286 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 287 | #endif // UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_ |