blob: f3ed604ca04be6087722fd3eecb03de1f8138e03 [file] [log] [blame]
Alex Deymo5e3ea272016-01-28 13:42:23 -08001//
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 Hassaniec7bc112020-10-29 16:47:58 -070017#ifndef UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_
18#define UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_
Alex Deymo5e3ea272016-01-28 13:42:23 -080019
20#include <stdint.h>
21
22#include <memory>
23#include <string>
24#include <vector>
25
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090026#include <android-base/unique_fd.h>
Alex Deymo5e3ea272016-01-28 13:42:23 -080027#include <base/time/time.h>
28
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000029#include "update_engine/aosp/apex_handler_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070030#include "update_engine/aosp/service_delegate_android_interface.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080031#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 Zhangc7a1d1f2022-07-29 13:36:29 -070034#include "update_engine/common/clock_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070035#include "update_engine/common/daemon_state_interface.h"
36#include "update_engine/common/download_action.h"
Kelvin Zhangf8441982022-12-07 18:18:47 -080037#include "update_engine/common/error_code.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080038#include "update_engine/common/hardware_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070039#include "update_engine/common/metrics_reporter_interface.h"
40#include "update_engine/common/network_selector_interface.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080041#include "update_engine/common/prefs_interface.h"
Kelvin Zhang70eef232020-06-12 20:32:40 +000042#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Alex Deymo0d298542016-03-30 18:31:49 -070043#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080044
45namespace chromeos_update_engine {
46
Kelvin Zhanga43d6e82021-05-26 10:14:42 -040047enum class OTAResult {
48 NOT_ATTEMPTED,
49 ROLLED_BACK,
50 UPDATED_NEED_REBOOT,
51 OTA_SUCCESSFUL,
52};
53
Alex Deymo0d298542016-03-30 18:31:49 -070054class UpdateAttempterAndroid
55 : public ServiceDelegateAndroidInterface,
56 public ActionProcessorDelegate,
57 public DownloadActionDelegate,
Kelvin Zhang70eef232020-06-12 20:32:40 +000058 public FilesystemVerifyDelegate,
Yifan Hong90965502020-02-19 15:22:47 -080059 public PostinstallRunnerAction::DelegateInterface,
60 public CleanupPreviousUpdateActionDelegateInterface {
Alex Deymo5e3ea272016-01-28 13:42:23 -080061 public:
62 using UpdateStatus = update_engine::UpdateStatus;
63
Alex Deymo03a4de72016-07-20 16:08:23 -070064 UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080065 PrefsInterface* prefs,
66 BootControlInterface* boot_control_,
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000067 HardwareInterface* hardware_,
68 std::unique_ptr<ApexHandlerInterface> apex_handler);
Alex Deymo5e3ea272016-01-28 13:42:23 -080069 ~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.Nam500ca132019-06-26 13:48:07 +090080 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 Deymo5e3ea272016-01-28 13:42:23 -080085 bool SuspendUpdate(brillo::ErrorPtr* error) override;
86 bool ResumeUpdate(brillo::ErrorPtr* error) override;
87 bool CancelUpdate(brillo::ErrorPtr* error) override;
Alex Deymo3b678db2016-02-09 11:50:06 -080088 bool ResetStatus(brillo::ErrorPtr* error) override;
Sen Jiang28d8ed92018-02-01 13:46:39 -080089 bool VerifyPayloadApplicable(const std::string& metadata_filename,
90 brillo::ErrorPtr* error) override;
Yifan Hong6f7e29f2019-12-13 14:41:06 -080091 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 Hong40bb0d02020-02-24 17:33:14 -080095 void CleanupSuccessfulUpdate(
96 std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface> callback,
97 brillo::ErrorPtr* error) override;
Tianjie7f8f2ab2021-07-23 17:08:50 -070098 bool setShouldSwitchSlotOnReboot(const std::string& metadata_filename,
99 brillo::ErrorPtr* error) override;
100 bool resetShouldSwitchSlotOnReboot(brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800101
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 Zhang70eef232020-06-12 20:32:40 +0000117 // FilesystemVerifyDelegate overrides
118 void OnVerifyProgressUpdate(double progress) override;
119
Alex Deymo0d298542016-03-30 18:31:49 -0700120 // PostinstallRunnerAction::DelegateInterface
121 void ProgressUpdate(double progress) override;
122
Yifan Hong90965502020-02-19 15:22:47 -0800123 // CleanupPreviousUpdateActionDelegateInterface
124 void OnCleanupProgressUpdate(double progress) override;
125
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400126 // 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 Zhang1304fe72021-10-06 19:12:12 -0700135 void set_update_certificates_path(
136 const std::string& update_certificates_path) {
137 update_certificates_path_ = update_certificates_path;
138 }
139
Alex Deymo5e3ea272016-01-28 13:42:23 -0800140 private:
Tianjie Xu90aaa102017-10-10 17:39:03 -0700141 friend class UpdateAttempterAndroidTest;
142
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400143 // 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 Deymo5e3ea272016-01-28 13:42:23 -0800149 // 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 Hassani667cf7b2018-07-25 14:32:00 -0700162 // applying an update using a given HttpFetcher. The ownership of |fetcher| is
163 // passed to this function.
164 void BuildUpdateActions(HttpFetcher* fetcher);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800165
Alex Deymo5e3ea272016-01-28 13:42:23 -0800166 // Writes to the processing completed marker. Does nothing if
167 // |update_completed_marker_| is empty.
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400168 [[nodiscard]] bool WriteUpdateCompletedMarker();
Alex Deymo5e3ea272016-01-28 13:42:23 -0800169
170 // Returns whether an update was completed in the current boot.
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400171 [[nodiscard]] bool UpdateCompletedOnThisBoot();
Alex Deymo5e3ea272016-01-28 13:42:23 -0800172
Tianjie Xu90aaa102017-10-10 17:39:03 -0700173 // 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 Xu2a0ea632018-08-06 12:59:23 -0700178 // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the
179 // current update.
180 // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of
181 // the current update.
Tianjie Xud4777a12017-10-24 14:54:18 -0700182 // |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 Xu90aaa102017-10-10 17:39:03 -0700186
187 // Metrics report function to call:
188 // |ReportUpdateAttemptMetrics|
189 // |ReportSuccessfulUpdateMetrics|
190 // Prefs to update:
191 // |kPrefsSystemUpdatedMarker|
192 void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
193
Kelvin Zhang4061c512021-05-25 13:42:55 -0400194 // 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 Xu90aaa102017-10-10 17:39:03 -0700198 // Metrics report function to call:
199 // |ReportAbnormallyTerminatedUpdateAttemptMetrics|
200 // |ReportTimeToRebootMetrics|
201 // Prefs to update:
202 // |kPrefsBootId|, |kPrefsPreviousVersion|
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400203 void UpdateStateAfterReboot(OTAResult result);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700204
205 // Prefs to update:
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700206 // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|,
207 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700208 void UpdatePrefsOnUpdateStart(bool is_resume);
209
210 // Prefs to delete:
xunchang9cf52622019-01-25 11:04:58 -0800211 // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded|
Tianjie Xud4777a12017-10-24 14:54:18 -0700212 // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
xunchang9cf52622019-01-25 11:04:58 -0800213 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700214 void ClearMetricsPrefs();
215
Yifan Hongbd47d622019-12-13 14:59:58 -0800216 // 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 Hong90965502020-02-19 15:22:47 -0800226 // Enqueue and run a CleanupPreviousUpdateAction.
227 void ScheduleCleanupPreviousUpdate();
228
Yifan Hong40bb0d02020-02-24 17:33:14 -0800229 // 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 Deymo03a4de72016-07-20 16:08:23 -0700236 DaemonStateInterface* daemon_state_;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800237
238 // DaemonStateAndroid pointers.
239 PrefsInterface* prefs_;
240 BootControlInterface* boot_control_;
241 HardwareInterface* hardware_;
242
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +0000243 std::unique_ptr<ApexHandlerInterface> apex_handler_android_;
244
Alex Deymo5e3ea272016-01-28 13:42:23 -0800245 // 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 Hassani667cf7b2018-07-25 14:32:00 -0700250 // The processor for running Actions.
Alex Deymo5e3ea272016-01-28 13:42:23 -0800251 std::unique_ptr<ActionProcessor> processor_;
252
Alex Deymo5e3ea272016-01-28 13:42:23 -0800253 // 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 Deymo0fd51ff2016-02-03 14:22:43 -0800260 // The offset in the payload file where the CrAU part starts.
261 int64_t base_offset_{0};
262
Alex Deymo87792ea2016-07-25 15:40:36 -0700263 // Helper class to select the network to use during the update.
264 std::unique_ptr<NetworkSelectorInterface> network_selector_;
265
Tianjie Xu90aaa102017-10-10 17:39:03 -0700266 std::unique_ptr<ClockInterface> clock_;
267
Tianjie Xu1b661142017-09-28 14:03:42 -0700268 std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
269
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900270 ::android::base::unique_fd payload_fd_;
271
Yifan Hong40bb0d02020-02-24 17:33:14 -0800272 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 Zhang1304fe72021-10-06 19:12:12 -0700278 // The path to the zip file with X509 certificates.
279 std::string update_certificates_path_{constants::kUpdateCertificatesPath};
Kelvin Zhangf8441982022-12-07 18:18:47 -0800280 ErrorCode last_error_{ErrorCode::kSuccess};
Kelvin Zhang1304fe72021-10-06 19:12:12 -0700281
Alex Deymo5e3ea272016-01-28 13:42:23 -0800282 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
283};
284
285} // namespace chromeos_update_engine
286
Amin Hassaniec7bc112020-10-29 16:47:58 -0700287#endif // UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_