xref: /aosp_15_r20/system/update_engine/download_action.cc (revision 5a9231315b4521097b8dc3750bc806fcafe0c72f)
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 <string>
20 
21 #include <base/files/file_path.h>
22 #include <base/metrics/statistics_recorder.h>
23 #include <android-base/stringprintf.h>
24 
25 #include "update_engine/common/boot_control_interface.h"
26 #include "update_engine/common/error_code_utils.h"
27 #include "update_engine/common/multi_range_http_fetcher.h"
28 #include "update_engine/common/prefs_interface.h"
29 #include "update_engine/common/utils.h"
30 
31 using base::FilePath;
32 using std::string;
33 
34 namespace chromeos_update_engine {
35 
DownloadAction(PrefsInterface * prefs,BootControlInterface * boot_control,HardwareInterface * hardware,HttpFetcher * http_fetcher,bool interactive,std::string update_certificates_path)36 DownloadAction::DownloadAction(PrefsInterface* prefs,
37                                BootControlInterface* boot_control,
38                                HardwareInterface* hardware,
39                                HttpFetcher* http_fetcher,
40                                bool interactive,
41                                std::string update_certificates_path)
42     : prefs_(prefs),
43       boot_control_(boot_control),
44       hardware_(hardware),
45       http_fetcher_(new MultiRangeHttpFetcher(http_fetcher)),
46       interactive_(interactive),
47       code_(ErrorCode::kSuccess),
48       delegate_(nullptr),
49       update_certificates_path_(std::move(update_certificates_path)) {}
50 
~DownloadAction()51 DownloadAction::~DownloadAction() {}
52 
PerformAction()53 void DownloadAction::PerformAction() {
54   http_fetcher_->set_delegate(this);
55 
56   // Get the InstallPlan and read it
57   CHECK(HasInputObject());
58   install_plan_ = GetInputObject();
59   install_plan_.Dump();
60 
61   bytes_received_ = 0;
62   bytes_received_previous_payloads_ = 0;
63   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   }
78   CHECK_GE(install_plan_.payloads.size(), 1UL);
79   if (!payload_)
80     payload_ = &install_plan_.payloads[0];
81 
82   LOG(INFO) << "Marking new slot as unbootable";
83   if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) {
84     LOG(WARNING) << "Unable to mark new slot "
85                  << BootControlInterface::SlotName(install_plan_.target_slot)
86                  << ". Proceeding with the update anyway.";
87   }
88 
89   StartDownloading();
90 }
91 
LoadCachedManifest(int64_t manifest_size)92 bool 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 
105   ErrorCode error{};
106   const bool success =
107       delta_performer_->Write(
108           cached_manifest_bytes.data(), cached_manifest_bytes.size(), &error) &&
109       delta_performer_->IsManifestValid();
110   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 
StartDownloading()120 void DownloadAction::StartDownloading() {
121   download_active_ = true;
122   http_fetcher_->ClearRanges();
123 
124   if (delta_performer_ != nullptr) {
125     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_,
133                                               interactive_,
134                                               update_certificates_path_));
135   }
136 
137   if (install_plan_.is_resume &&
138       payload_ == &install_plan_.payloads[resume_payload_index_]) {
139     // Resuming an update so parse the cached manifest first
140     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);
144 
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
149         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_);
158       }
159       http_fetcher_->AddRange(base_offset_,
160                               manifest_metadata_size + manifest_signature_size);
161     }
162 
163     // 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.
166     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 
186   http_fetcher_->BeginTransfer(install_plan_.download_url);
187 }
188 
SuspendAction()189 void DownloadAction::SuspendAction() {
190   http_fetcher_->Pause();
191 }
192 
ResumeAction()193 void DownloadAction::ResumeAction() {
194   http_fetcher_->Unpause();
195 }
196 
TerminateProcessing()197 void DownloadAction::TerminateProcessing() {
198   if (delta_performer_) {
199     delta_performer_->Close();
200     delta_performer_.reset();
201   }
202   download_active_ = false;
203   // Terminates the transfer. The action is terminated, if necessary, when the
204   // TransferTerminated callback is received.
205   http_fetcher_->TerminateTransfer();
206 }
207 
SeekToOffset(off_t offset)208 void DownloadAction::SeekToOffset(off_t offset) {
209   bytes_received_ = offset;
210 }
211 
ReceivedBytes(HttpFetcher * fetcher,const void * bytes,size_t length)212 bool DownloadAction::ReceivedBytes(HttpFetcher* fetcher,
213                                    const void* bytes,
214                                    size_t length) {
215   bytes_received_ += length;
216   uint64_t bytes_downloaded_total =
217       bytes_received_previous_payloads_ + bytes_received_;
218   if (delegate_ && download_active_) {
219     delegate_->BytesReceived(
220         length, bytes_downloaded_total - base_offset_, bytes_total_);
221   }
222   if (delta_performer_ && !delta_performer_->Write(bytes, length, &code_)) {
223     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";
227     } else {
228       LOG(ERROR) << "Unknown error in DeltaPerformer's Write method when "
229                  << "processing the received payload -- Terminating processing";
230       code_ = ErrorCode::kDownloadWriteError;
231     }
232     // 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.
235     TerminateProcessing();
236     return false;
237   }
238 
239   return true;
240 }
241 
TransferComplete(HttpFetcher * fetcher,bool successful)242 void DownloadAction::TransferComplete(HttpFetcher* fetcher, bool successful) {
243   if (delta_performer_) {
244     LOG_IF(WARNING, delta_performer_->Close() != 0)
245         << "Error closing the writer.";
246   }
247   download_active_ = false;
248   ErrorCode code =
249       successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
250   if (code == ErrorCode::kSuccess) {
251     if (delta_performer_ && !payload_->already_applied)
252       code = delta_performer_->VerifyPayload(payload_->hash, payload_->size);
253     if (code == ErrorCode::kSuccess) {
254       CHECK_EQ(install_plan_.payloads.size(), 1UL);
255       // All payloads have been applied and verified.
256       if (delegate_)
257         delegate_->DownloadComplete();
258 
259       // Log UpdateEngine.DownloadAction.* histograms to help diagnose
260       // long-blocking operations.
261       std::string histogram_output;
262       base::StatisticsRecorder::WriteGraph("UpdateEngine.DownloadAction.",
263                                            &histogram_output);
264       LOG(INFO) << histogram_output;
265     } else {
266       LOG(ERROR) << "Download of " << install_plan_.download_url
267                  << " failed due to payload verification error.";
268     }
269   }
270 
271   // Write the path to the output pipe if we're successful.
272   if (code == ErrorCode::kSuccess && HasOutputPipe())
273     SetOutputObject(install_plan_);
274   processor_->ActionComplete(this, code);
275 }
276 
TransferTerminated(HttpFetcher * fetcher)277 void DownloadAction::TransferTerminated(HttpFetcher* fetcher) {
278   if (code_ != ErrorCode::kSuccess) {
279     processor_->ActionComplete(this, code_);
280   } 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);
284   }
285 }
286 
287 }  // namespace chromeos_update_engine
288