1 //
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 //
16
17 #include "update_engine/common/download_action.h"
18
19 #include <errno.h>
20
21 #include <algorithm>
22 #include <string>
23
24 #include <base/files/file_path.h>
25 #include <base/metrics/statistics_recorder.h>
26 #include <base/strings/stringprintf.h>
27
28 #include "update_engine/common/boot_control_interface.h"
29 #include "update_engine/common/error_code_utils.h"
30 #include "update_engine/common/multi_range_http_fetcher.h"
31 #include "update_engine/common/prefs_interface.h"
32 #include "update_engine/common/utils.h"
33
34 using base::FilePath;
35 using std::string;
36
37 namespace chromeos_update_engine {
38
DownloadAction(PrefsInterface * prefs,BootControlInterface * boot_control,HardwareInterface * hardware,HttpFetcher * http_fetcher,bool interactive,std::string update_certificates_path)39 DownloadAction::DownloadAction(PrefsInterface* prefs,
40 BootControlInterface* boot_control,
41 HardwareInterface* hardware,
42 HttpFetcher* http_fetcher,
43 bool interactive,
44 std::string update_certificates_path)
45 : prefs_(prefs),
46 boot_control_(boot_control),
47 hardware_(hardware),
48 http_fetcher_(new MultiRangeHttpFetcher(http_fetcher)),
49 interactive_(interactive),
50 code_(ErrorCode::kSuccess),
51 delegate_(nullptr),
52 update_certificates_path_(std::move(update_certificates_path)) {}
53
~DownloadAction()54 DownloadAction::~DownloadAction() {}
55
PerformAction()56 void DownloadAction::PerformAction() {
57 http_fetcher_->set_delegate(this);
58
59 // Get the InstallPlan and read it
60 CHECK(HasInputObject());
61 install_plan_ = GetInputObject();
62 install_plan_.Dump();
63
64 bytes_received_ = 0;
65 bytes_received_previous_payloads_ = 0;
66 bytes_total_ = 0;
67 for (const auto& payload : install_plan_.payloads)
68 bytes_total_ += payload.size;
69
70 if (install_plan_.is_resume) {
71 int64_t payload_index = 0;
72 if (prefs_->GetInt64(kPrefsUpdateStatePayloadIndex, &payload_index) &&
73 static_cast<size_t>(payload_index) < install_plan_.payloads.size()) {
74 // Save the index for the resume payload before downloading any previous
75 // payload, otherwise it will be overwritten.
76 resume_payload_index_ = payload_index;
77 for (int i = 0; i < payload_index; i++)
78 install_plan_.payloads[i].already_applied = true;
79 }
80 }
81 CHECK_GE(install_plan_.payloads.size(), 1UL);
82 if (!payload_)
83 payload_ = &install_plan_.payloads[0];
84
85 LOG(INFO) << "Marking new slot as unbootable";
86 if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) {
87 LOG(WARNING) << "Unable to mark new slot "
88 << BootControlInterface::SlotName(install_plan_.target_slot)
89 << ". Proceeding with the update anyway.";
90 }
91
92 StartDownloading();
93 }
94
LoadCachedManifest(int64_t manifest_size)95 bool DownloadAction::LoadCachedManifest(int64_t manifest_size) {
96 std::string cached_manifest_bytes;
97 if (!prefs_->GetString(kPrefsManifestBytes, &cached_manifest_bytes) ||
98 cached_manifest_bytes.size() <= 0) {
99 LOG(INFO) << "Cached Manifest data not found";
100 return false;
101 }
102 if (static_cast<int64_t>(cached_manifest_bytes.size()) != manifest_size) {
103 LOG(WARNING) << "Cached metadata has unexpected size: "
104 << cached_manifest_bytes.size() << " vs. " << manifest_size;
105 return false;
106 }
107
108 ErrorCode error{};
109 const bool success =
110 delta_performer_->Write(
111 cached_manifest_bytes.data(), cached_manifest_bytes.size(), &error) &&
112 delta_performer_->IsManifestValid();
113 if (success) {
114 LOG(INFO) << "Successfully parsed cached manifest";
115 } else {
116 // If parsing of cached data failed, fall back to fetch them using HTTP
117 LOG(WARNING) << "Cached manifest data fails to load, error code:"
118 << static_cast<int>(error) << "," << error;
119 }
120 return success;
121 }
122
StartDownloading()123 void DownloadAction::StartDownloading() {
124 download_active_ = true;
125 http_fetcher_->ClearRanges();
126
127 if (delta_performer_ != nullptr) {
128 LOG(INFO) << "Using writer for test.";
129 } else {
130 delta_performer_.reset(new DeltaPerformer(prefs_,
131 boot_control_,
132 hardware_,
133 delegate_,
134 &install_plan_,
135 payload_,
136 interactive_,
137 update_certificates_path_));
138 }
139
140 if (install_plan_.is_resume &&
141 payload_ == &install_plan_.payloads[resume_payload_index_]) {
142 // Resuming an update so parse the cached manifest first
143 int64_t manifest_metadata_size = 0;
144 int64_t manifest_signature_size = 0;
145 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
146 prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
147
148 // TODO(zhangkelvin) Add unittest for success and fallback route
149 if (!LoadCachedManifest(manifest_metadata_size + manifest_signature_size)) {
150 if (delta_performer_) {
151 // Create a new DeltaPerformer to reset all its state
152 delta_performer_ =
153 std::make_unique<DeltaPerformer>(prefs_,
154 boot_control_,
155 hardware_,
156 delegate_,
157 &install_plan_,
158 payload_,
159 interactive_,
160 update_certificates_path_);
161 }
162 http_fetcher_->AddRange(base_offset_,
163 manifest_metadata_size + manifest_signature_size);
164 }
165
166 // If there're remaining unprocessed data blobs, fetch them. Be careful
167 // not to request data beyond the end of the payload to avoid 416 HTTP
168 // response error codes.
169 int64_t next_data_offset = 0;
170 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
171 uint64_t resume_offset =
172 manifest_metadata_size + manifest_signature_size + next_data_offset;
173 if (!payload_->size) {
174 http_fetcher_->AddRange(base_offset_ + resume_offset);
175 } else if (resume_offset < payload_->size) {
176 http_fetcher_->AddRange(base_offset_ + resume_offset,
177 payload_->size - resume_offset);
178 }
179 } else {
180 if (payload_->size) {
181 http_fetcher_->AddRange(base_offset_, payload_->size);
182 } else {
183 // If no payload size is passed we assume we read until the end of the
184 // stream.
185 http_fetcher_->AddRange(base_offset_);
186 }
187 }
188
189 http_fetcher_->BeginTransfer(install_plan_.download_url);
190 }
191
SuspendAction()192 void DownloadAction::SuspendAction() {
193 http_fetcher_->Pause();
194 }
195
ResumeAction()196 void DownloadAction::ResumeAction() {
197 http_fetcher_->Unpause();
198 }
199
TerminateProcessing()200 void DownloadAction::TerminateProcessing() {
201 if (delta_performer_) {
202 delta_performer_->Close();
203 delta_performer_.reset();
204 }
205 download_active_ = false;
206 // Terminates the transfer. The action is terminated, if necessary, when the
207 // TransferTerminated callback is received.
208 http_fetcher_->TerminateTransfer();
209 }
210
SeekToOffset(off_t offset)211 void DownloadAction::SeekToOffset(off_t offset) {
212 bytes_received_ = offset;
213 }
214
ReceivedBytes(HttpFetcher * fetcher,const void * bytes,size_t length)215 bool DownloadAction::ReceivedBytes(HttpFetcher* fetcher,
216 const void* bytes,
217 size_t length) {
218 bytes_received_ += length;
219 uint64_t bytes_downloaded_total =
220 bytes_received_previous_payloads_ + bytes_received_;
221 if (delegate_ && download_active_) {
222 delegate_->BytesReceived(
223 length, bytes_downloaded_total - base_offset_, bytes_total_);
224 }
225 if (delta_performer_ && !delta_performer_->Write(bytes, length, &code_)) {
226 if (code_ != ErrorCode::kSuccess) {
227 LOG(ERROR) << "Error " << utils::ErrorCodeToString(code_) << " (" << code_
228 << ") in DeltaPerformer's Write method when "
229 << "processing the received payload -- Terminating processing";
230 } else {
231 LOG(ERROR) << "Unknown error in DeltaPerformer's Write method when "
232 << "processing the received payload -- Terminating processing";
233 code_ = ErrorCode::kDownloadWriteError;
234 }
235 // Don't tell the action processor that the action is complete until we get
236 // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
237 // objects may get destroyed before all callbacks are complete.
238 TerminateProcessing();
239 return false;
240 }
241
242 return true;
243 }
244
TransferComplete(HttpFetcher * fetcher,bool successful)245 void DownloadAction::TransferComplete(HttpFetcher* fetcher, bool successful) {
246 if (delta_performer_) {
247 LOG_IF(WARNING, delta_performer_->Close() != 0)
248 << "Error closing the writer.";
249 }
250 download_active_ = false;
251 ErrorCode code =
252 successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
253 if (code == ErrorCode::kSuccess) {
254 if (delta_performer_ && !payload_->already_applied)
255 code = delta_performer_->VerifyPayload(payload_->hash, payload_->size);
256 if (code == ErrorCode::kSuccess) {
257 CHECK_EQ(install_plan_.payloads.size(), 1UL);
258 // All payloads have been applied and verified.
259 if (delegate_)
260 delegate_->DownloadComplete();
261
262 // Log UpdateEngine.DownloadAction.* histograms to help diagnose
263 // long-blocking operations.
264 std::string histogram_output;
265 base::StatisticsRecorder::WriteGraph("UpdateEngine.DownloadAction.",
266 &histogram_output);
267 LOG(INFO) << histogram_output;
268 } else {
269 LOG(ERROR) << "Download of " << install_plan_.download_url
270 << " failed due to payload verification error.";
271 }
272 }
273
274 // Write the path to the output pipe if we're successful.
275 if (code == ErrorCode::kSuccess && HasOutputPipe())
276 SetOutputObject(install_plan_);
277 processor_->ActionComplete(this, code);
278 }
279
TransferTerminated(HttpFetcher * fetcher)280 void DownloadAction::TransferTerminated(HttpFetcher* fetcher) {
281 if (code_ != ErrorCode::kSuccess) {
282 processor_->ActionComplete(this, code_);
283 } else if (payload_->already_applied) {
284 LOG(INFO) << "TransferTerminated with ErrorCode::kSuccess when the current "
285 "payload has already applied, treating as TransferComplete.";
286 TransferComplete(fetcher, true);
287 }
288 }
289
290 } // namespace chromeos_update_engine
291