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