blob: 36331789a67fffbfcb3950a49c119d0f16faa574 [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"
Tianjie Xu90aaa102017-10-10 17:39:03 -070034#include "update_engine/common/clock.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"
Alex Deymo5e3ea272016-01-28 13:42:23 -080037#include "update_engine/common/hardware_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070038#include "update_engine/common/metrics_reporter_interface.h"
39#include "update_engine/common/network_selector_interface.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080040#include "update_engine/common/prefs_interface.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070041#include "update_engine/common/service_observer_interface.h"
Tianjie Xu90aaa102017-10-10 17:39:03 -070042#include "update_engine/metrics_utils.h"
Kelvin Zhang70eef232020-06-12 20:32:40 +000043#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Alex Deymo0d298542016-03-30 18:31:49 -070044#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080045
46namespace chromeos_update_engine {
47
Kelvin Zhanga43d6e82021-05-26 10:14:42 -040048enum class OTAResult {
49 NOT_ATTEMPTED,
50 ROLLED_BACK,
51 UPDATED_NEED_REBOOT,
52 OTA_SUCCESSFUL,
53};
54
Alex Deymo0d298542016-03-30 18:31:49 -070055class UpdateAttempterAndroid
56 : public ServiceDelegateAndroidInterface,
57 public ActionProcessorDelegate,
58 public DownloadActionDelegate,
Kelvin Zhang70eef232020-06-12 20:32:40 +000059 public FilesystemVerifyDelegate,
Yifan Hong90965502020-02-19 15:22:47 -080060 public PostinstallRunnerAction::DelegateInterface,
61 public CleanupPreviousUpdateActionDelegateInterface {
Alex Deymo5e3ea272016-01-28 13:42:23 -080062 public:
63 using UpdateStatus = update_engine::UpdateStatus;
64
Alex Deymo03a4de72016-07-20 16:08:23 -070065 UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080066 PrefsInterface* prefs,
67 BootControlInterface* boot_control_,
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000068 HardwareInterface* hardware_,
69 std::unique_ptr<ApexHandlerInterface> apex_handler);
Alex Deymo5e3ea272016-01-28 13:42:23 -080070 ~UpdateAttempterAndroid() override;
71
72 // Further initialization to be done post construction.
73 void Init();
74
75 // ServiceDelegateAndroidInterface overrides.
76 bool ApplyPayload(const std::string& payload_url,
77 int64_t payload_offset,
78 int64_t payload_size,
79 const std::vector<std::string>& key_value_pair_headers,
80 brillo::ErrorPtr* error) override;
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090081 bool ApplyPayload(int fd,
82 int64_t payload_offset,
83 int64_t payload_size,
84 const std::vector<std::string>& key_value_pair_headers,
85 brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080086 bool SuspendUpdate(brillo::ErrorPtr* error) override;
87 bool ResumeUpdate(brillo::ErrorPtr* error) override;
88 bool CancelUpdate(brillo::ErrorPtr* error) override;
Alex Deymo3b678db2016-02-09 11:50:06 -080089 bool ResetStatus(brillo::ErrorPtr* error) override;
Sen Jiang28d8ed92018-02-01 13:46:39 -080090 bool VerifyPayloadApplicable(const std::string& metadata_filename,
91 brillo::ErrorPtr* error) override;
Yifan Hong6f7e29f2019-12-13 14:41:06 -080092 uint64_t AllocateSpaceForPayload(
93 const std::string& metadata_filename,
94 const std::vector<std::string>& key_value_pair_headers,
95 brillo::ErrorPtr* error) override;
Yifan Hong40bb0d02020-02-24 17:33:14 -080096 void CleanupSuccessfulUpdate(
97 std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface> callback,
98 brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080099
100 // ActionProcessorDelegate methods:
101 void ProcessingDone(const ActionProcessor* processor,
102 ErrorCode code) override;
103 void ProcessingStopped(const ActionProcessor* processor) override;
104 void ActionCompleted(ActionProcessor* processor,
105 AbstractAction* action,
106 ErrorCode code) override;
107
108 // DownloadActionDelegate overrides.
109 void BytesReceived(uint64_t bytes_progressed,
110 uint64_t bytes_received,
111 uint64_t total) override;
112 bool ShouldCancel(ErrorCode* cancel_reason) override;
113 void DownloadComplete() override;
114
Kelvin Zhang70eef232020-06-12 20:32:40 +0000115 // FilesystemVerifyDelegate overrides
116 void OnVerifyProgressUpdate(double progress) override;
117
Alex Deymo0d298542016-03-30 18:31:49 -0700118 // PostinstallRunnerAction::DelegateInterface
119 void ProgressUpdate(double progress) override;
120
Yifan Hong90965502020-02-19 15:22:47 -0800121 // CleanupPreviousUpdateActionDelegateInterface
122 void OnCleanupProgressUpdate(double progress) override;
123
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400124 // Check the result of an OTA update. Intended to be called after reboot, this
125 // will use prefs on disk to determine if OTA was installed, or rolledback.
126 [[nodiscard]] OTAResult GetOTAUpdateResult() const;
127 // Intended to be called:
128 // 1. When system rebooted and slot switch is attempted
129 // 2. When a new update is started
130 // 3. When user called |ResetStatus()|
131 bool ClearUpdateCompletedMarker();
132
Alex Deymo5e3ea272016-01-28 13:42:23 -0800133 private:
Tianjie Xu90aaa102017-10-10 17:39:03 -0700134 friend class UpdateAttempterAndroidTest;
135
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400136 // Return |true| only if slot switched successfully after an OTA reboot.
137 // This will return |false| if an downgrade OTA is applied. Because after a
138 // downgrade OTA, we wipe /data, and there's no way for update_engine to
139 // "remember" that a downgrade OTA took place.
140 [[nodiscard]] bool OTARebootSucceeded() const;
141
Alex Deymo5e3ea272016-01-28 13:42:23 -0800142 // Schedules an event loop callback to start the action processor. This is
143 // scheduled asynchronously to unblock the event loop.
144 void ScheduleProcessingStart();
145
146 // Notifies an update request completed with the given error |code| to all
147 // observers.
148 void TerminateUpdateAndNotify(ErrorCode error_code);
149
150 // Sets the status to the given |status| and notifies a status update to
151 // all observers.
152 void SetStatusAndNotify(UpdateStatus status);
153
154 // Helper method to construct the sequence of actions to be performed for
Amin Hassani667cf7b2018-07-25 14:32:00 -0700155 // applying an update using a given HttpFetcher. The ownership of |fetcher| is
156 // passed to this function.
157 void BuildUpdateActions(HttpFetcher* fetcher);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800158
Alex Deymo5e3ea272016-01-28 13:42:23 -0800159 // Writes to the processing completed marker. Does nothing if
160 // |update_completed_marker_| is empty.
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400161 [[nodiscard]] bool WriteUpdateCompletedMarker();
Alex Deymo5e3ea272016-01-28 13:42:23 -0800162
163 // Returns whether an update was completed in the current boot.
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400164 [[nodiscard]] bool UpdateCompletedOnThisBoot();
Alex Deymo5e3ea272016-01-28 13:42:23 -0800165
Tianjie Xu90aaa102017-10-10 17:39:03 -0700166 // Prefs to use for metrics report
167 // |kPrefsPayloadAttemptNumber|: number of update attempts for the current
168 // payload_id.
169 // |KprefsNumReboots|: number of reboots when applying the current update.
170 // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update.
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700171 // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the
172 // current update.
173 // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of
174 // the current update.
Tianjie Xud4777a12017-10-24 14:54:18 -0700175 // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current
176 // payload_id.
177 // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since
178 // the last successful update.
Tianjie Xu90aaa102017-10-10 17:39:03 -0700179
180 // Metrics report function to call:
181 // |ReportUpdateAttemptMetrics|
182 // |ReportSuccessfulUpdateMetrics|
183 // Prefs to update:
184 // |kPrefsSystemUpdatedMarker|
185 void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
186
Kelvin Zhang4061c512021-05-25 13:42:55 -0400187 // This function is called after update_engine is started after device
188 // reboots. If update_engine is restarted w/o device reboot, this function
189 // would not be called.
190
Tianjie Xu90aaa102017-10-10 17:39:03 -0700191 // Metrics report function to call:
192 // |ReportAbnormallyTerminatedUpdateAttemptMetrics|
193 // |ReportTimeToRebootMetrics|
194 // Prefs to update:
195 // |kPrefsBootId|, |kPrefsPreviousVersion|
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400196 void UpdateStateAfterReboot(OTAResult result);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700197
198 // Prefs to update:
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700199 // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|,
200 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700201 void UpdatePrefsOnUpdateStart(bool is_resume);
202
203 // Prefs to delete:
xunchang9cf52622019-01-25 11:04:58 -0800204 // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded|
Tianjie Xud4777a12017-10-24 14:54:18 -0700205 // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
xunchang9cf52622019-01-25 11:04:58 -0800206 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700207 void ClearMetricsPrefs();
208
Yifan Hongbd47d622019-12-13 14:59:58 -0800209 // Return source and target slots for update.
210 BootControlInterface::Slot GetCurrentSlot() const;
211 BootControlInterface::Slot GetTargetSlot() const;
212
213 // Helper of public VerifyPayloadApplicable. Return the parsed manifest in
214 // |manifest|.
215 static bool VerifyPayloadParseManifest(const std::string& metadata_filename,
216 DeltaArchiveManifest* manifest,
217 brillo::ErrorPtr* error);
218
Yifan Hong90965502020-02-19 15:22:47 -0800219 // Enqueue and run a CleanupPreviousUpdateAction.
220 void ScheduleCleanupPreviousUpdate();
221
Yifan Hong40bb0d02020-02-24 17:33:14 -0800222 // Notify and clear |cleanup_previous_update_callbacks_|.
223 void NotifyCleanupPreviousUpdateCallbacksAndClear();
224
225 // Remove |callback| from |cleanup_previous_update_callbacks_|.
226 void RemoveCleanupPreviousUpdateCallback(
227 CleanupSuccessfulUpdateCallbackInterface* callback);
228
Alex Deymo03a4de72016-07-20 16:08:23 -0700229 DaemonStateInterface* daemon_state_;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800230
231 // DaemonStateAndroid pointers.
232 PrefsInterface* prefs_;
233 BootControlInterface* boot_control_;
234 HardwareInterface* hardware_;
235
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +0000236 std::unique_ptr<ApexHandlerInterface> apex_handler_android_;
237
Alex Deymo5e3ea272016-01-28 13:42:23 -0800238 // Last status notification timestamp used for throttling. Use monotonic
239 // TimeTicks to ensure that notifications are sent even if the system clock is
240 // set back in the middle of an update.
241 base::TimeTicks last_notify_time_;
242
Amin Hassani04d41622018-12-20 15:35:41 -0800243 // Only direct proxy supported.
244 DirectProxyResolver proxy_resolver_;
245
Amin Hassani667cf7b2018-07-25 14:32:00 -0700246 // The processor for running Actions.
Alex Deymo5e3ea272016-01-28 13:42:23 -0800247 std::unique_ptr<ActionProcessor> processor_;
248
Alex Deymo5e3ea272016-01-28 13:42:23 -0800249 // The InstallPlan used during the ongoing update.
250 InstallPlan install_plan_;
251
252 // For status:
253 UpdateStatus status_{UpdateStatus::IDLE};
254 double download_progress_{0.0};
255
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800256 // The offset in the payload file where the CrAU part starts.
257 int64_t base_offset_{0};
258
Alex Deymo87792ea2016-07-25 15:40:36 -0700259 // Helper class to select the network to use during the update.
260 std::unique_ptr<NetworkSelectorInterface> network_selector_;
261
Tianjie Xu90aaa102017-10-10 17:39:03 -0700262 std::unique_ptr<ClockInterface> clock_;
263
Tianjie Xu1b661142017-09-28 14:03:42 -0700264 std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
265
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900266 ::android::base::unique_fd payload_fd_;
267
Yifan Hong40bb0d02020-02-24 17:33:14 -0800268 std::vector<std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface>>
269 cleanup_previous_update_callbacks_;
270 // Result of previous CleanupPreviousUpdateAction. Nullopt If
271 // CleanupPreviousUpdateAction has not been executed.
272 std::optional<ErrorCode> cleanup_previous_update_code_{std::nullopt};
273
Alex Deymo5e3ea272016-01-28 13:42:23 -0800274 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
275};
276
277} // namespace chromeos_update_engine
278
Amin Hassaniec7bc112020-10-29 16:47:58 -0700279#endif // UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_