blob: 9a8c87062bc5bd8b5745cd681c8342abbcacf2a4 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 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//
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/common/download_action.h"
Alex Deymoaab50e32014-11-10 19:55:35 -080018
Andrew de los Reyesf9714432010-05-04 10:21:23 -070019#include <string>
David Zeuthen8f191b22013-08-06 12:27:50 -070020
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/files/file_path.h>
Lann Martin39f57142017-07-14 09:18:42 -060022#include <base/metrics/statistics_recorder.h>
Kelvin Zhangb9a9aa22024-10-15 10:38:35 -070023#include <android-base/stringprintf.h>
David Zeuthen8f191b22013-08-06 12:27:50 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/boot_control_interface.h"
Alex Deymoed9cc182016-06-15 16:19:29 -070026#include "update_engine/common/error_code_utils.h"
Sen Jiang5ae865b2017-04-18 14:24:40 -070027#include "update_engine/common/multi_range_http_fetcher.h"
Kelvin Zhang5c1451f2020-12-02 17:01:54 -050028#include "update_engine/common/prefs_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/utils.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000030
Alex Deymof329b932014-10-30 01:37:48 -070031using base::FilePath;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070032using std::string;
rspangler@google.com49fdf182009-10-10 00:57:34 +000033
34namespace chromeos_update_engine {
35
Darin Petkov73058b42010-10-06 16:32:19 -070036DownloadAction::DownloadAction(PrefsInterface* prefs,
Alex Deymo1b3556c2016-02-03 09:54:02 -080037 BootControlInterface* boot_control,
38 HardwareInterface* hardware,
Amin Hassani7ecda262017-07-11 17:10:50 -070039 HttpFetcher* http_fetcher,
Kelvin Zhang1304fe72021-10-06 19:12:12 -070040 bool interactive,
41 std::string update_certificates_path)
Darin Petkov73058b42010-10-06 16:32:19 -070042 : prefs_(prefs),
Alex Deymo1b3556c2016-02-03 09:54:02 -080043 boot_control_(boot_control),
44 hardware_(hardware),
Sen Jiang5ae865b2017-04-18 14:24:40 -070045 http_fetcher_(new MultiRangeHttpFetcher(http_fetcher)),
Amin Hassanied37d682018-04-06 13:22:00 -070046 interactive_(interactive),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070047 code_(ErrorCode::kSuccess),
Kelvin Zhang1304fe72021-10-06 19:12:12 -070048 delegate_(nullptr),
49 update_certificates_path_(std::move(update_certificates_path)) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000050
51DownloadAction::~DownloadAction() {}
52
53void DownloadAction::PerformAction() {
54 http_fetcher_->set_delegate(this);
rspangler@google.com49fdf182009-10-10 00:57:34 +000055
adlr@google.comc98a7ed2009-12-04 18:54:03 +000056 // Get the InstallPlan and read it
57 CHECK(HasInputObject());
Andrew de los Reyesf9185172010-05-03 11:07:05 -070058 install_plan_ = GetInputObject();
Andrew de los Reyesf9185172010-05-03 11:07:05 -070059 install_plan_.Dump();
Sen Jiang5ae865b2017-04-18 14:24:40 -070060
61 bytes_received_ = 0;
Aaron Wood9321f502017-09-07 11:18:54 -070062 bytes_received_previous_payloads_ = 0;
Sen Jiang5ae865b2017-04-18 14:24:40 -070063 bytes_total_ = 0;
64 for (const auto& payload : install_plan_.payloads)
65 bytes_total_ += payload.size;
66
67 if (install_plan_.is_resume) {
68 int64_t payload_index = 0;
69 if (prefs_->GetInt64(kPrefsUpdateStatePayloadIndex, &payload_index) &&
70 static_cast<size_t>(payload_index) < install_plan_.payloads.size()) {
71 // Save the index for the resume payload before downloading any previous
72 // payload, otherwise it will be overwritten.
73 resume_payload_index_ = payload_index;
74 for (int i = 0; i < payload_index; i++)
75 install_plan_.payloads[i].already_applied = true;
76 }
77 }
Kelvin Zhang5c1451f2020-12-02 17:01:54 -050078 CHECK_GE(install_plan_.payloads.size(), 1UL);
Sen Jiang0affc2c2017-02-10 15:55:05 -080079 if (!payload_)
80 payload_ = &install_plan_.payloads[0];
adlr@google.comc98a7ed2009-12-04 18:54:03 +000081
Alex Deymo5ed695e2015-10-05 16:59:23 -070082 LOG(INFO) << "Marking new slot as unbootable";
Alex Deymo1b3556c2016-02-03 09:54:02 -080083 if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) {
Alex Deymo5ed695e2015-10-05 16:59:23 -070084 LOG(WARNING) << "Unable to mark new slot "
85 << BootControlInterface::SlotName(install_plan_.target_slot)
86 << ". Proceeding with the update anyway.";
87 }
88
Sen Jiang6c736682017-03-10 15:01:36 -080089 StartDownloading();
90}
91
Kelvin Zhangcc011d32020-07-10 18:20:08 -040092bool DownloadAction::LoadCachedManifest(int64_t manifest_size) {
93 std::string cached_manifest_bytes;
94 if (!prefs_->GetString(kPrefsManifestBytes, &cached_manifest_bytes) ||
95 cached_manifest_bytes.size() <= 0) {
96 LOG(INFO) << "Cached Manifest data not found";
97 return false;
98 }
99 if (static_cast<int64_t>(cached_manifest_bytes.size()) != manifest_size) {
100 LOG(WARNING) << "Cached metadata has unexpected size: "
101 << cached_manifest_bytes.size() << " vs. " << manifest_size;
102 return false;
103 }
104
Daniel Zhengda4f7292022-09-02 22:59:32 +0000105 ErrorCode error{};
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400106 const bool success =
Kelvin Zhang22b62e42020-12-14 14:50:40 -0500107 delta_performer_->Write(
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400108 cached_manifest_bytes.data(), cached_manifest_bytes.size(), &error) &&
Kelvin Zhang22b62e42020-12-14 14:50:40 -0500109 delta_performer_->IsManifestValid();
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400110 if (success) {
111 LOG(INFO) << "Successfully parsed cached manifest";
112 } else {
113 // If parsing of cached data failed, fall back to fetch them using HTTP
114 LOG(WARNING) << "Cached manifest data fails to load, error code:"
115 << static_cast<int>(error) << "," << error;
116 }
117 return success;
118}
119
Sen Jiang6c736682017-03-10 15:01:36 -0800120void DownloadAction::StartDownloading() {
Sen Jiang5ae865b2017-04-18 14:24:40 -0700121 download_active_ = true;
122 http_fetcher_->ClearRanges();
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400123
Kelvin Zhang22b62e42020-12-14 14:50:40 -0500124 if (delta_performer_ != nullptr) {
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400125 LOG(INFO) << "Using writer for test.";
126 } else {
127 delta_performer_.reset(new DeltaPerformer(prefs_,
128 boot_control_,
129 hardware_,
130 delegate_,
131 &install_plan_,
132 payload_,
Kelvin Zhang1304fe72021-10-06 19:12:12 -0700133 interactive_,
134 update_certificates_path_));
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400135 }
136
Sen Jiang5ae865b2017-04-18 14:24:40 -0700137 if (install_plan_.is_resume &&
138 payload_ == &install_plan_.payloads[resume_payload_index_]) {
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400139 // Resuming an update so parse the cached manifest first
Sen Jiang5ae865b2017-04-18 14:24:40 -0700140 int64_t manifest_metadata_size = 0;
141 int64_t manifest_signature_size = 0;
142 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
143 prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400144
145 // TODO(zhangkelvin) Add unittest for success and fallback route
146 if (!LoadCachedManifest(manifest_metadata_size + manifest_signature_size)) {
147 if (delta_performer_) {
148 // Create a new DeltaPerformer to reset all its state
Kelvin Zhang1304fe72021-10-06 19:12:12 -0700149 delta_performer_ =
150 std::make_unique<DeltaPerformer>(prefs_,
151 boot_control_,
152 hardware_,
153 delegate_,
154 &install_plan_,
155 payload_,
156 interactive_,
157 update_certificates_path_);
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400158 }
159 http_fetcher_->AddRange(base_offset_,
160 manifest_metadata_size + manifest_signature_size);
161 }
162
Kelvin Zhangead9fd72020-12-03 12:57:35 -0500163 // If there're remaining unprocessed data blobs, fetch them. Be careful
164 // not to request data beyond the end of the payload to avoid 416 HTTP
165 // response error codes.
Sen Jiang5ae865b2017-04-18 14:24:40 -0700166 int64_t next_data_offset = 0;
167 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
168 uint64_t resume_offset =
169 manifest_metadata_size + manifest_signature_size + next_data_offset;
170 if (!payload_->size) {
171 http_fetcher_->AddRange(base_offset_ + resume_offset);
172 } else if (resume_offset < payload_->size) {
173 http_fetcher_->AddRange(base_offset_ + resume_offset,
174 payload_->size - resume_offset);
175 }
176 } else {
177 if (payload_->size) {
178 http_fetcher_->AddRange(base_offset_, payload_->size);
179 } else {
180 // If no payload size is passed we assume we read until the end of the
181 // stream.
182 http_fetcher_->AddRange(base_offset_);
183 }
184 }
185
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700186 http_fetcher_->BeginTransfer(install_plan_.download_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000187}
188
Alex Deymof2858572016-02-25 11:20:13 -0800189void DownloadAction::SuspendAction() {
190 http_fetcher_->Pause();
191}
192
193void DownloadAction::ResumeAction() {
194 http_fetcher_->Unpause();
195}
196
rspangler@google.com49fdf182009-10-10 00:57:34 +0000197void DownloadAction::TerminateProcessing() {
Kelvin Zhang22b62e42020-12-14 14:50:40 -0500198 if (delta_performer_) {
199 delta_performer_->Close();
200 delta_performer_.reset();
Darin Petkov698d0412010-10-13 10:59:44 -0700201 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800202 download_active_ = false;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800203 // Terminates the transfer. The action is terminated, if necessary, when the
204 // TransferTerminated callback is received.
205 http_fetcher_->TerminateTransfer();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000206}
207
Andrew de los Reyes34e41a12010-10-26 20:07:58 -0700208void DownloadAction::SeekToOffset(off_t offset) {
209 bytes_received_ = offset;
210}
211
Amin Hassani0cd9d772018-07-31 23:55:43 -0700212bool DownloadAction::ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800213 const void* bytes,
214 size_t length) {
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700215 bytes_received_ += length;
Aaron Wood9321f502017-09-07 11:18:54 -0700216 uint64_t bytes_downloaded_total =
217 bytes_received_previous_payloads_ + bytes_received_;
Alex Deymo542c19b2015-12-03 07:43:31 -0300218 if (delegate_ && download_active_) {
Kelvin Zhang5440fe32023-12-12 10:04:37 -0800219 delegate_->BytesReceived(
220 length, bytes_downloaded_total - base_offset_, bytes_total_);
Alex Deymo542c19b2015-12-03 07:43:31 -0300221 }
Kelvin Zhang22b62e42020-12-14 14:50:40 -0500222 if (delta_performer_ && !delta_performer_->Write(bytes, length, &code_)) {
Sen Jiang5ae865b2017-04-18 14:24:40 -0700223 if (code_ != ErrorCode::kSuccess) {
224 LOG(ERROR) << "Error " << utils::ErrorCodeToString(code_) << " (" << code_
225 << ") in DeltaPerformer's Write method when "
226 << "processing the received payload -- Terminating processing";
Kelvin Zhang430852d2023-05-25 17:00:24 -0700227 } else {
228 LOG(ERROR) << "Unknown error in DeltaPerformer's Write method when "
229 << "processing the received payload -- Terminating processing";
230 code_ = ErrorCode::kDownloadWriteError;
Sen Jiang5ae865b2017-04-18 14:24:40 -0700231 }
Darin Petkov9ce452b2010-11-17 14:33:28 -0800232 // Don't tell the action processor that the action is complete until we get
233 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
234 // objects may get destroyed before all callbacks are complete.
Darin Petkov698d0412010-10-13 10:59:44 -0700235 TerminateProcessing();
Amin Hassani0cd9d772018-07-31 23:55:43 -0700236 return false;
Darin Petkov698d0412010-10-13 10:59:44 -0700237 }
David Zeuthen8f191b22013-08-06 12:27:50 -0700238
Amin Hassani0cd9d772018-07-31 23:55:43 -0700239 return true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000240}
241
Alex Deymo60ca1a72015-06-18 18:19:15 -0700242void DownloadAction::TransferComplete(HttpFetcher* fetcher, bool successful) {
Kelvin Zhang22b62e42020-12-14 14:50:40 -0500243 if (delta_performer_) {
244 LOG_IF(WARNING, delta_performer_->Close() != 0)
245 << "Error closing the writer.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000246 }
Alex Deymo1b3556c2016-02-03 09:54:02 -0800247 download_active_ = false;
David Zeuthena99981f2013-04-29 13:42:47 -0700248 ErrorCode code =
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700249 successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
Aaron Wood9321f502017-09-07 11:18:54 -0700250 if (code == ErrorCode::kSuccess) {
251 if (delta_performer_ && !payload_->already_applied)
Sen Jiang5ae865b2017-04-18 14:24:40 -0700252 code = delta_performer_->VerifyPayload(payload_->hash, payload_->size);
Lann Martin39f57142017-07-14 09:18:42 -0600253 if (code == ErrorCode::kSuccess) {
Kelvin Zhang5c1451f2020-12-02 17:01:54 -0500254 CHECK_EQ(install_plan_.payloads.size(), 1UL);
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700255 // All payloads have been applied and verified.
256 if (delegate_)
257 delegate_->DownloadComplete();
258
Lann Martin39f57142017-07-14 09:18:42 -0600259 // Log UpdateEngine.DownloadAction.* histograms to help diagnose
Sen Jiang7ebfccf2018-03-15 13:55:55 -0700260 // long-blocking operations.
Lann Martin39f57142017-07-14 09:18:42 -0600261 std::string histogram_output;
Amin Hassani008c4582019-01-13 16:22:47 -0800262 base::StatisticsRecorder::WriteGraph("UpdateEngine.DownloadAction.",
263 &histogram_output);
Lann Martin39f57142017-07-14 09:18:42 -0600264 LOG(INFO) << histogram_output;
265 } else {
Darin Petkov7ed561b2011-10-04 02:59:03 -0700266 LOG(ERROR) << "Download of " << install_plan_.download_url
267 << " failed due to payload verification error.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000268 }
269 }
Darin Petkovc1a8b422010-07-19 11:34:49 -0700270
Darin Petkovc97435c2010-07-20 12:37:43 -0700271 // Write the path to the output pipe if we're successful.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700272 if (code == ErrorCode::kSuccess && HasOutputPipe())
Darin Petkov3aefa862010-12-07 14:45:00 -0800273 SetOutputObject(install_plan_);
Darin Petkovc97435c2010-07-20 12:37:43 -0700274 processor_->ActionComplete(this, code);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000275}
276
Sen Jiang5ae865b2017-04-18 14:24:40 -0700277void DownloadAction::TransferTerminated(HttpFetcher* fetcher) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700278 if (code_ != ErrorCode::kSuccess) {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800279 processor_->ActionComplete(this, code_);
Sen Jiang5ae865b2017-04-18 14:24:40 -0700280 } else if (payload_->already_applied) {
281 LOG(INFO) << "TransferTerminated with ErrorCode::kSuccess when the current "
282 "payload has already applied, treating as TransferComplete.";
283 TransferComplete(fetcher, true);
Darin Petkov9ce452b2010-11-17 14:33:28 -0800284 }
285}
286
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700287} // namespace chromeos_update_engine