blob: f3feed06e9f1c4fb1e58f96d3fe227a1fb28f3f4 [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"
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"
Kelvin Zhang70eef232020-06-12 20:32:40 +000041#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Alex Deymo0d298542016-03-30 18:31:49 -070042#include "update_engine/payload_consumer/postinstall_runner_action.h"
Alex Deymo5e3ea272016-01-28 13:42:23 -080043
44namespace chromeos_update_engine {
45
Kelvin Zhanga43d6e82021-05-26 10:14:42 -040046enum class OTAResult {
47 NOT_ATTEMPTED,
48 ROLLED_BACK,
49 UPDATED_NEED_REBOOT,
50 OTA_SUCCESSFUL,
51};
52
Alex Deymo0d298542016-03-30 18:31:49 -070053class UpdateAttempterAndroid
54 : public ServiceDelegateAndroidInterface,
55 public ActionProcessorDelegate,
56 public DownloadActionDelegate,
Kelvin Zhang70eef232020-06-12 20:32:40 +000057 public FilesystemVerifyDelegate,
Yifan Hong90965502020-02-19 15:22:47 -080058 public PostinstallRunnerAction::DelegateInterface,
59 public CleanupPreviousUpdateActionDelegateInterface {
Alex Deymo5e3ea272016-01-28 13:42:23 -080060 public:
61 using UpdateStatus = update_engine::UpdateStatus;
62
Alex Deymo03a4de72016-07-20 16:08:23 -070063 UpdateAttempterAndroid(DaemonStateInterface* daemon_state,
Alex Deymo5e3ea272016-01-28 13:42:23 -080064 PrefsInterface* prefs,
65 BootControlInterface* boot_control_,
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +000066 HardwareInterface* hardware_,
67 std::unique_ptr<ApexHandlerInterface> apex_handler);
Alex Deymo5e3ea272016-01-28 13:42:23 -080068 ~UpdateAttempterAndroid() override;
69
70 // Further initialization to be done post construction.
71 void Init();
72
73 // ServiceDelegateAndroidInterface overrides.
74 bool ApplyPayload(const std::string& payload_url,
75 int64_t payload_offset,
76 int64_t payload_size,
77 const std::vector<std::string>& key_value_pair_headers,
78 brillo::ErrorPtr* error) override;
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090079 bool ApplyPayload(int fd,
80 int64_t payload_offset,
81 int64_t payload_size,
82 const std::vector<std::string>& key_value_pair_headers,
83 brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -080084 bool SuspendUpdate(brillo::ErrorPtr* error) override;
85 bool ResumeUpdate(brillo::ErrorPtr* error) override;
86 bool CancelUpdate(brillo::ErrorPtr* error) override;
Alex Deymo3b678db2016-02-09 11:50:06 -080087 bool ResetStatus(brillo::ErrorPtr* error) override;
Sen Jiang28d8ed92018-02-01 13:46:39 -080088 bool VerifyPayloadApplicable(const std::string& metadata_filename,
89 brillo::ErrorPtr* error) override;
Yifan Hong6f7e29f2019-12-13 14:41:06 -080090 uint64_t AllocateSpaceForPayload(
91 const std::string& metadata_filename,
92 const std::vector<std::string>& key_value_pair_headers,
93 brillo::ErrorPtr* error) override;
Yifan Hong40bb0d02020-02-24 17:33:14 -080094 void CleanupSuccessfulUpdate(
95 std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface> callback,
96 brillo::ErrorPtr* error) override;
Tianjie7f8f2ab2021-07-23 17:08:50 -070097 bool setShouldSwitchSlotOnReboot(const std::string& metadata_filename,
98 brillo::ErrorPtr* error) override;
99 bool resetShouldSwitchSlotOnReboot(brillo::ErrorPtr* error) override;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800100
101 // ActionProcessorDelegate methods:
102 void ProcessingDone(const ActionProcessor* processor,
103 ErrorCode code) override;
104 void ProcessingStopped(const ActionProcessor* processor) override;
105 void ActionCompleted(ActionProcessor* processor,
106 AbstractAction* action,
107 ErrorCode code) override;
108
109 // DownloadActionDelegate overrides.
110 void BytesReceived(uint64_t bytes_progressed,
111 uint64_t bytes_received,
112 uint64_t total) override;
113 bool ShouldCancel(ErrorCode* cancel_reason) override;
114 void DownloadComplete() override;
115
Kelvin Zhang70eef232020-06-12 20:32:40 +0000116 // FilesystemVerifyDelegate overrides
117 void OnVerifyProgressUpdate(double progress) override;
118
Alex Deymo0d298542016-03-30 18:31:49 -0700119 // PostinstallRunnerAction::DelegateInterface
120 void ProgressUpdate(double progress) override;
121
Yifan Hong90965502020-02-19 15:22:47 -0800122 // CleanupPreviousUpdateActionDelegateInterface
123 void OnCleanupProgressUpdate(double progress) override;
124
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400125 // Check the result of an OTA update. Intended to be called after reboot, this
126 // will use prefs on disk to determine if OTA was installed, or rolledback.
127 [[nodiscard]] OTAResult GetOTAUpdateResult() const;
128 // Intended to be called:
129 // 1. When system rebooted and slot switch is attempted
130 // 2. When a new update is started
131 // 3. When user called |ResetStatus()|
132 bool ClearUpdateCompletedMarker();
133
Kelvin Zhang1304fe72021-10-06 19:12:12 -0700134 void set_update_certificates_path(
135 const std::string& update_certificates_path) {
136 update_certificates_path_ = update_certificates_path;
137 }
138
Alex Deymo5e3ea272016-01-28 13:42:23 -0800139 private:
Tianjie Xu90aaa102017-10-10 17:39:03 -0700140 friend class UpdateAttempterAndroidTest;
141
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400142 // Return |true| only if slot switched successfully after an OTA reboot.
143 // This will return |false| if an downgrade OTA is applied. Because after a
144 // downgrade OTA, we wipe /data, and there's no way for update_engine to
145 // "remember" that a downgrade OTA took place.
146 [[nodiscard]] bool OTARebootSucceeded() const;
147
Alex Deymo5e3ea272016-01-28 13:42:23 -0800148 // Schedules an event loop callback to start the action processor. This is
149 // scheduled asynchronously to unblock the event loop.
150 void ScheduleProcessingStart();
151
152 // Notifies an update request completed with the given error |code| to all
153 // observers.
154 void TerminateUpdateAndNotify(ErrorCode error_code);
155
156 // Sets the status to the given |status| and notifies a status update to
157 // all observers.
158 void SetStatusAndNotify(UpdateStatus status);
159
160 // Helper method to construct the sequence of actions to be performed for
Amin Hassani667cf7b2018-07-25 14:32:00 -0700161 // applying an update using a given HttpFetcher. The ownership of |fetcher| is
162 // passed to this function.
163 void BuildUpdateActions(HttpFetcher* fetcher);
Alex Deymo5e3ea272016-01-28 13:42:23 -0800164
Alex Deymo5e3ea272016-01-28 13:42:23 -0800165 // Writes to the processing completed marker. Does nothing if
166 // |update_completed_marker_| is empty.
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400167 [[nodiscard]] bool WriteUpdateCompletedMarker();
Alex Deymo5e3ea272016-01-28 13:42:23 -0800168
169 // Returns whether an update was completed in the current boot.
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400170 [[nodiscard]] bool UpdateCompletedOnThisBoot();
Alex Deymo5e3ea272016-01-28 13:42:23 -0800171
Tianjie Xu90aaa102017-10-10 17:39:03 -0700172 // Prefs to use for metrics report
173 // |kPrefsPayloadAttemptNumber|: number of update attempts for the current
174 // payload_id.
175 // |KprefsNumReboots|: number of reboots when applying the current update.
176 // |kPrefsSystemUpdatedMarker|: end timestamp of the last successful update.
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700177 // |kPrefsUpdateTimestampStart|: start timestamp in monotonic time of the
178 // current update.
179 // |kPrefsUpdateBootTimestampStart|: start timestamp in boot time of
180 // the current update.
Tianjie Xud4777a12017-10-24 14:54:18 -0700181 // |kPrefsCurrentBytesDownloaded|: number of bytes downloaded for the current
182 // payload_id.
183 // |kPrefsTotalBytesDownloaded|: number of bytes downloaded in total since
184 // the last successful update.
Tianjie Xu90aaa102017-10-10 17:39:03 -0700185
186 // Metrics report function to call:
187 // |ReportUpdateAttemptMetrics|
188 // |ReportSuccessfulUpdateMetrics|
189 // Prefs to update:
190 // |kPrefsSystemUpdatedMarker|
191 void CollectAndReportUpdateMetricsOnUpdateFinished(ErrorCode error_code);
192
Kelvin Zhang4061c512021-05-25 13:42:55 -0400193 // This function is called after update_engine is started after device
194 // reboots. If update_engine is restarted w/o device reboot, this function
195 // would not be called.
196
Tianjie Xu90aaa102017-10-10 17:39:03 -0700197 // Metrics report function to call:
198 // |ReportAbnormallyTerminatedUpdateAttemptMetrics|
199 // |ReportTimeToRebootMetrics|
200 // Prefs to update:
201 // |kPrefsBootId|, |kPrefsPreviousVersion|
Kelvin Zhanga43d6e82021-05-26 10:14:42 -0400202 void UpdateStateAfterReboot(OTAResult result);
Tianjie Xu90aaa102017-10-10 17:39:03 -0700203
204 // Prefs to update:
Tianjie Xu2a0ea632018-08-06 12:59:23 -0700205 // |kPrefsPayloadAttemptNumber|, |kPrefsUpdateTimestampStart|,
206 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700207 void UpdatePrefsOnUpdateStart(bool is_resume);
208
209 // Prefs to delete:
xunchang9cf52622019-01-25 11:04:58 -0800210 // |kPrefsNumReboots|, |kPrefsCurrentBytesDownloaded|
Tianjie Xud4777a12017-10-24 14:54:18 -0700211 // |kPrefsSystemUpdatedMarker|, |kPrefsUpdateTimestampStart|,
xunchang9cf52622019-01-25 11:04:58 -0800212 // |kPrefsUpdateBootTimestampStart|
Tianjie Xu90aaa102017-10-10 17:39:03 -0700213 void ClearMetricsPrefs();
214
Yifan Hongbd47d622019-12-13 14:59:58 -0800215 // Return source and target slots for update.
216 BootControlInterface::Slot GetCurrentSlot() const;
217 BootControlInterface::Slot GetTargetSlot() const;
218
219 // Helper of public VerifyPayloadApplicable. Return the parsed manifest in
220 // |manifest|.
221 static bool VerifyPayloadParseManifest(const std::string& metadata_filename,
222 DeltaArchiveManifest* manifest,
223 brillo::ErrorPtr* error);
224
Yifan Hong90965502020-02-19 15:22:47 -0800225 // Enqueue and run a CleanupPreviousUpdateAction.
226 void ScheduleCleanupPreviousUpdate();
227
Yifan Hong40bb0d02020-02-24 17:33:14 -0800228 // Notify and clear |cleanup_previous_update_callbacks_|.
229 void NotifyCleanupPreviousUpdateCallbacksAndClear();
230
231 // Remove |callback| from |cleanup_previous_update_callbacks_|.
232 void RemoveCleanupPreviousUpdateCallback(
233 CleanupSuccessfulUpdateCallbackInterface* callback);
234
Alex Deymo03a4de72016-07-20 16:08:23 -0700235 DaemonStateInterface* daemon_state_;
Alex Deymo5e3ea272016-01-28 13:42:23 -0800236
237 // DaemonStateAndroid pointers.
238 PrefsInterface* prefs_;
239 BootControlInterface* boot_control_;
240 HardwareInterface* hardware_;
241
Mohammad Samiul Islam24a82792021-02-12 16:52:36 +0000242 std::unique_ptr<ApexHandlerInterface> apex_handler_android_;
243
Alex Deymo5e3ea272016-01-28 13:42:23 -0800244 // Last status notification timestamp used for throttling. Use monotonic
245 // TimeTicks to ensure that notifications are sent even if the system clock is
246 // set back in the middle of an update.
247 base::TimeTicks last_notify_time_;
248
Amin Hassani667cf7b2018-07-25 14:32:00 -0700249 // The processor for running Actions.
Alex Deymo5e3ea272016-01-28 13:42:23 -0800250 std::unique_ptr<ActionProcessor> processor_;
251
Alex Deymo5e3ea272016-01-28 13:42:23 -0800252 // The InstallPlan used during the ongoing update.
253 InstallPlan install_plan_;
254
255 // For status:
256 UpdateStatus status_{UpdateStatus::IDLE};
257 double download_progress_{0.0};
258
Alex Deymo0fd51ff2016-02-03 14:22:43 -0800259 // The offset in the payload file where the CrAU part starts.
260 int64_t base_offset_{0};
261
Alex Deymo87792ea2016-07-25 15:40:36 -0700262 // Helper class to select the network to use during the update.
263 std::unique_ptr<NetworkSelectorInterface> network_selector_;
264
Tianjie Xu90aaa102017-10-10 17:39:03 -0700265 std::unique_ptr<ClockInterface> clock_;
266
Tianjie Xu1b661142017-09-28 14:03:42 -0700267 std::unique_ptr<MetricsReporterInterface> metrics_reporter_;
268
Kyeongkab.Nam500ca132019-06-26 13:48:07 +0900269 ::android::base::unique_fd payload_fd_;
270
Yifan Hong40bb0d02020-02-24 17:33:14 -0800271 std::vector<std::unique_ptr<CleanupSuccessfulUpdateCallbackInterface>>
272 cleanup_previous_update_callbacks_;
273 // Result of previous CleanupPreviousUpdateAction. Nullopt If
274 // CleanupPreviousUpdateAction has not been executed.
275 std::optional<ErrorCode> cleanup_previous_update_code_{std::nullopt};
276
Kelvin Zhang1304fe72021-10-06 19:12:12 -0700277 // The path to the zip file with X509 certificates.
278 std::string update_certificates_path_{constants::kUpdateCertificatesPath};
279
Alex Deymo5e3ea272016-01-28 13:42:23 -0800280 DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
281};
282
283} // namespace chromeos_update_engine
284
Amin Hassaniec7bc112020-10-29 16:47:58 -0700285#endif // UPDATE_ENGINE_AOSP_UPDATE_ATTEMPTER_ANDROID_H_