blob: 45103e65f6b85c982cecf58943ee9003a8f47347 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/delta_performer.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070018
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070019#include <errno.h>
Alex Deymo79715ad2015-10-02 14:27:53 -070020#include <linux/fs.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -070021
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022#include <algorithm>
23#include <cstring>
Yifan Hong537802d2018-08-15 13:15:42 -070024#include <map>
Ben Chan02f7c1d2014-10-18 15:18:02 -070025#include <memory>
Tianjied3865d12020-06-03 15:25:17 -070026#include <set>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070027#include <string>
Amin Hassanid87304c2017-08-29 11:40:03 -070028#include <utility>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070029#include <vector>
30
Kelvin Zhangcfc531f2022-08-24 17:58:53 +000031#include <android-base/properties.h>
Ben Chan06c76a42014-09-05 08:21:06 -070032#include <base/files/file_util.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070033#include <base/format_macros.h>
Lann Martin39f57142017-07-14 09:18:42 -060034#include <base/metrics/histogram_macros.h>
Alex Deymo67140842016-06-15 13:28:59 -070035#include <base/strings/string_number_conversions.h>
Kelvin Zhangcfc531f2022-08-24 17:58:53 +000036#include <base/strings/stringprintf.h>
Lann Martin39f57142017-07-14 09:18:42 -060037#include <base/time/time.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070038#include <brillo/data_encoding.h>
Alex Deymo6765a682017-05-19 13:13:54 -070039#include <bsdiff/bspatch.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070040#include <google/protobuf/repeated_field.h>
Amin Hassani02855c22017-09-06 22:34:50 -070041#include <puffin/puffpatch.h>
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070042
Alex Deymo39910dc2015-11-09 17:04:30 -080043#include "update_engine/common/constants.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070044#include "update_engine/common/download_action.h"
Yifan Hong87029332020-09-01 17:20:08 -070045#include "update_engine/common/error_code.h"
46#include "update_engine/common/error_code_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080047#include "update_engine/common/hardware_interface.h"
Alex Deymo542c19b2015-12-03 07:43:31 -030048#include "update_engine/common/prefs_interface.h"
Alex Deymo542c19b2015-12-03 07:43:31 -030049#include "update_engine/common/terminator.h"
Kelvin Zhang52cb1d72020-10-27 13:44:25 -040050#include "update_engine/common/utils.h"
Tianjied3865d12020-06-03 15:25:17 -070051#include "update_engine/payload_consumer/partition_update_generator_interface.h"
Kelvin Zhang50bac652020-09-28 15:51:41 -040052#include "update_engine/payload_consumer/partition_writer.h"
Alex Deymo51c264e2016-11-04 15:49:53 -070053#if USE_FEC
54#include "update_engine/payload_consumer/fec_file_descriptor.h"
55#endif // USE_FEC
Alex Deymo39910dc2015-11-09 17:04:30 -080056#include "update_engine/payload_consumer/payload_constants.h"
57#include "update_engine/payload_consumer/payload_verifier.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070058
Alex Deymo161c4a12014-05-16 15:56:21 -070059using google::protobuf::RepeatedPtrField;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070060using std::min;
61using std::string;
62using std::vector;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070063
64namespace chromeos_update_engine {
Gilad Arnold8a86fa52013-01-15 12:35:05 -080065const unsigned DeltaPerformer::kProgressLogMaxChunks = 10;
66const unsigned DeltaPerformer::kProgressLogTimeoutSeconds = 30;
67const unsigned DeltaPerformer::kProgressDownloadWeight = 50;
68const unsigned DeltaPerformer::kProgressOperationsWeight = 50;
Colin Howes0e452c92018-11-02 13:18:44 -070069const uint64_t DeltaPerformer::kCheckpointFrequencySeconds = 1;
Darin Petkovabc7bc02011-02-23 14:39:43 -080070
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070071namespace {
Darin Petkov73058b42010-10-06 16:32:19 -070072const int kUpdateStateOperationInvalid = -1;
Darin Petkov61426142010-10-08 11:04:55 -070073const int kMaxResumedUpdateFailures = 10;
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080074
Alex Vakulenkod2779df2014-06-16 13:19:00 -070075} // namespace
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070076
Gilad Arnold8a86fa52013-01-15 12:35:05 -080077// Computes the ratio of |part| and |total|, scaled to |norm|, using integer
78// arithmetic.
79static uint64_t IntRatio(uint64_t part, uint64_t total, uint64_t norm) {
80 return part * norm / total;
81}
82
83void DeltaPerformer::LogProgress(const char* message_prefix) {
84 // Format operations total count and percentage.
85 string total_operations_str("?");
86 string completed_percentage_str("");
87 if (num_total_operations_) {
Alex Deymoc00c98a2015-03-17 17:38:00 -070088 total_operations_str = std::to_string(num_total_operations_);
Gilad Arnold8a86fa52013-01-15 12:35:05 -080089 // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
Amin Hassani008c4582019-01-13 16:22:47 -080090 completed_percentage_str = base::StringPrintf(
91 " (%" PRIu64 "%%)",
92 IntRatio(next_operation_num_, num_total_operations_, 100));
Gilad Arnold8a86fa52013-01-15 12:35:05 -080093 }
94
95 // Format download total count and percentage.
Sen Jiang0affc2c2017-02-10 15:55:05 -080096 size_t payload_size = payload_->size;
Gilad Arnold8a86fa52013-01-15 12:35:05 -080097 string payload_size_str("?");
98 string downloaded_percentage_str("");
99 if (payload_size) {
Alex Deymoc00c98a2015-03-17 17:38:00 -0700100 payload_size_str = std::to_string(payload_size);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800101 // Upcasting to 64-bit to avoid overflow, back to size_t for formatting.
Amin Hassani008c4582019-01-13 16:22:47 -0800102 downloaded_percentage_str = base::StringPrintf(
103 " (%" PRIu64 "%%)", IntRatio(total_bytes_received_, payload_size, 100));
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800104 }
105
106 LOG(INFO) << (message_prefix ? message_prefix : "") << next_operation_num_
107 << "/" << total_operations_str << " operations"
Amin Hassani008c4582019-01-13 16:22:47 -0800108 << completed_percentage_str << ", " << total_bytes_received_ << "/"
109 << payload_size_str << " bytes downloaded"
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800110 << downloaded_percentage_str << ", overall progress "
111 << overall_progress_ << "%";
112}
113
114void DeltaPerformer::UpdateOverallProgress(bool force_log,
115 const char* message_prefix) {
116 // Compute our download and overall progress.
117 unsigned new_overall_progress = 0;
Alex Vakulenko0103c362016-01-20 07:56:15 -0800118 static_assert(kProgressDownloadWeight + kProgressOperationsWeight == 100,
119 "Progress weights don't add up");
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800120 // Only consider download progress if its total size is known; otherwise
121 // adjust the operations weight to compensate for the absence of download
122 // progress. Also, make sure to cap the download portion at
123 // kProgressDownloadWeight, in case we end up downloading more than we
124 // initially expected (this indicates a problem, but could generally happen).
125 // TODO(garnold) the correction of operations weight when we do not have the
126 // total payload size, as well as the conditional guard below, should both be
127 // eliminated once we ensure that the payload_size in the install plan is
128 // always given and is non-zero. This currently isn't the case during unit
129 // tests (see chromium-os:37969).
Sen Jiang0affc2c2017-02-10 15:55:05 -0800130 size_t payload_size = payload_->size;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800131 unsigned actual_operations_weight = kProgressOperationsWeight;
132 if (payload_size)
Amin Hassani008c4582019-01-13 16:22:47 -0800133 new_overall_progress +=
134 min(static_cast<unsigned>(IntRatio(
135 total_bytes_received_, payload_size, kProgressDownloadWeight)),
136 kProgressDownloadWeight);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800137 else
138 actual_operations_weight += kProgressDownloadWeight;
139
140 // Only add completed operations if their total number is known; we definitely
141 // expect an update to have at least one operation, so the expectation is that
142 // this will eventually reach |actual_operations_weight|.
143 if (num_total_operations_)
Amin Hassani008c4582019-01-13 16:22:47 -0800144 new_overall_progress += IntRatio(
145 next_operation_num_, num_total_operations_, actual_operations_weight);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800146
147 // Progress ratio cannot recede, unless our assumptions about the total
148 // payload size, total number of operations, or the monotonicity of progress
149 // is breached.
150 if (new_overall_progress < overall_progress_) {
151 LOG(WARNING) << "progress counter receded from " << overall_progress_
152 << "% down to " << new_overall_progress << "%; this is a bug";
153 force_log = true;
154 }
155 overall_progress_ = new_overall_progress;
156
157 // Update chunk index, log as needed: if forced by called, or we completed a
158 // progress chunk, or a timeout has expired.
Sen Jiang53c2ec52019-01-10 15:27:59 -0800159 base::TimeTicks curr_time = base::TimeTicks::Now();
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800160 unsigned curr_progress_chunk =
161 overall_progress_ * kProgressLogMaxChunks / 100;
162 if (force_log || curr_progress_chunk > last_progress_chunk_ ||
163 curr_time > forced_progress_log_time_) {
164 forced_progress_log_time_ = curr_time + forced_progress_log_wait_;
165 LogProgress(message_prefix);
166 }
167 last_progress_chunk_ = curr_progress_chunk;
168}
169
Amin Hassani008c4582019-01-13 16:22:47 -0800170size_t DeltaPerformer::CopyDataToBuffer(const char** bytes_p,
171 size_t* count_p,
Gilad Arnoldfe133932014-01-14 12:25:50 -0800172 size_t max) {
173 const size_t count = *count_p;
174 if (!count)
175 return 0; // Special case shortcut.
Alex Deymof329b932014-10-30 01:37:48 -0700176 size_t read_len = min(count, max - buffer_.size());
Gilad Arnoldfe133932014-01-14 12:25:50 -0800177 const char* bytes_start = *bytes_p;
178 const char* bytes_end = bytes_start + read_len;
Sen Jiangbe19a242017-11-17 11:48:17 -0800179 buffer_.reserve(max);
Gilad Arnoldfe133932014-01-14 12:25:50 -0800180 buffer_.insert(buffer_.end(), bytes_start, bytes_end);
181 *bytes_p = bytes_end;
182 *count_p = count - read_len;
183 return read_len;
184}
185
Amin Hassani008c4582019-01-13 16:22:47 -0800186bool DeltaPerformer::HandleOpResult(bool op_result,
187 const char* op_type_name,
Gilad Arnoldfe133932014-01-14 12:25:50 -0800188 ErrorCode* error) {
189 if (op_result)
190 return true;
191
192 LOG(ERROR) << "Failed to perform " << op_type_name << " operation "
Alex Deymo3d009062016-05-13 15:51:25 -0700193 << next_operation_num_ << ", which is the operation "
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400194 << GetPartitionOperationNum() << " in partition \""
Alex Deymo3d009062016-05-13 15:51:25 -0700195 << partitions_[current_partition_].partition_name() << "\"";
Sen Jiangbe2c47b2016-06-15 14:09:27 -0700196 if (*error == ErrorCode::kSuccess)
197 *error = ErrorCode::kDownloadOperationExecutionError;
Gilad Arnoldfe133932014-01-14 12:25:50 -0800198 return false;
199}
200
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700201int DeltaPerformer::Close() {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700202 int err = -CloseCurrentPartition();
Amin Hassani008c4582019-01-13 16:22:47 -0800203 LOG_IF(ERROR,
204 !payload_hash_calculator_.Finalize() ||
205 !signed_hash_calculator_.Finalize())
Sen Jiangf6813802015-11-03 21:27:29 -0800206 << "Unable to finalize the hash.";
Darin Petkov934bb412010-11-18 11:21:35 -0800207 if (!buffer_.empty()) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700208 LOG(INFO) << "Discarding " << buffer_.size() << " unused downloaded bytes";
209 if (err >= 0)
Darin Petkov934bb412010-11-18 11:21:35 -0800210 err = 1;
Darin Petkov934bb412010-11-18 11:21:35 -0800211 }
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700212 return -err;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700213}
214
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700215int DeltaPerformer::CloseCurrentPartition() {
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400216 if (!partition_writer_) {
217 return 0;
218 }
219 int err = partition_writer_->Close();
220 partition_writer_ = nullptr;
221 return err;
222}
223
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700224bool DeltaPerformer::OpenCurrentPartition() {
225 if (current_partition_ >= partitions_.size())
226 return false;
227
228 const PartitionUpdate& partition = partitions_[current_partition_];
Sen Jiangd2ff2a02017-04-06 14:53:31 -0700229 size_t num_previous_partitions =
230 install_plan_->partitions.size() - partitions_.size();
231 const InstallPlan::Partition& install_part =
232 install_plan_->partitions[num_previous_partitions + current_partition_];
Kelvin Zhang94f51cc2020-09-25 11:34:49 -0400233 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500234 partition_writer_ = CreatePartitionWriter(
235 partition,
236 install_part,
237 dynamic_control,
238 block_size_,
239 interactive_,
240 IsDynamicPartition(install_part.name, install_plan_->target_slot));
Tianjie55abd3c2020-06-19 00:22:59 -0700241 // Open source fds if we have a delta payload, or for partitions in the
242 // partial update.
Kelvin Zhangdf3c1952021-10-06 18:56:55 -0700243 const bool source_may_exist = manifest_.partial_update() ||
244 payload_->type == InstallPayloadType::kDelta;
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400245 const size_t partition_operation_num = GetPartitionOperationNum();
246
247 TEST_AND_RETURN_FALSE(partition_writer_->Init(
248 install_plan_, source_may_exist, partition_operation_num));
249 CheckpointUpdateProgress(true);
250 return true;
251}
252
253size_t DeltaPerformer::GetPartitionOperationNum() {
254 return next_operation_num_ -
255 (current_partition_ ? acc_num_operations_[current_partition_ - 1] : 0);
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400256}
257
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700258namespace {
259
260void LogPartitionInfoHash(const PartitionInfo& info, const string& tag) {
Kelvin Zhang3fe49642021-10-04 15:35:02 -0700261 string sha256 = HexEncode(info.hash());
Alex Vakulenko981a9fb2015-02-09 12:51:24 -0800262 LOG(INFO) << "PartitionInfo " << tag << " sha256: " << sha256
263 << " size: " << info.size();
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700264}
265
Alex Deymo39910dc2015-11-09 17:04:30 -0800266void LogPartitionInfo(const vector<PartitionUpdate>& partitions) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700267 for (const PartitionUpdate& partition : partitions) {
Sen Jiang7b9a5872018-01-17 13:25:06 -0800268 if (partition.has_old_partition_info()) {
269 LogPartitionInfoHash(partition.old_partition_info(),
270 "old " + partition.partition_name());
271 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700272 LogPartitionInfoHash(partition.new_partition_info(),
273 "new " + partition.partition_name());
274 }
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700275}
276
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700277} // namespace
Andrew de los Reyes89f17be2010-10-22 13:39:09 -0700278
Sen Jiangb8060e42015-09-24 17:30:50 -0700279bool DeltaPerformer::IsHeaderParsed() const {
280 return metadata_size_ != 0;
281}
Jay Srinivasanf4318702012-09-24 11:56:24 -0700282
Sen Jiang9c89e842018-02-02 13:51:21 -0800283MetadataParseResult DeltaPerformer::ParsePayloadMetadata(
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700284 const brillo::Blob& payload, ErrorCode* error) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700285 *error = ErrorCode::kSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700286
Sen Jiangb8060e42015-09-24 17:30:50 -0700287 if (!IsHeaderParsed()) {
Sen Jiangf1236632018-05-11 16:03:23 -0700288 MetadataParseResult result =
289 payload_metadata_.ParsePayloadHeader(payload, error);
Sen Jiang9c89e842018-02-02 13:51:21 -0800290 if (result != MetadataParseResult::kSuccess)
291 return result;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700292
Sen Jiang9c89e842018-02-02 13:51:21 -0800293 metadata_size_ = payload_metadata_.GetMetadataSize();
294 metadata_signature_size_ = payload_metadata_.GetMetadataSignatureSize();
295 major_payload_version_ = payload_metadata_.GetMajorVersion();
Gilad Arnoldfe133932014-01-14 12:25:50 -0800296
297 // If the metadata size is present in install plan, check for it immediately
298 // even before waiting for that many number of bytes to be downloaded in the
299 // payload. This will prevent any attack which relies on us downloading data
300 // beyond the expected metadata size.
Gilad Arnoldfe133932014-01-14 12:25:50 -0800301 if (install_plan_->hash_checks_mandatory) {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800302 if (payload_->metadata_size != metadata_size_) {
Gilad Arnoldfe133932014-01-14 12:25:50 -0800303 LOG(ERROR) << "Mandatory metadata size in Omaha response ("
Sen Jiang0affc2c2017-02-10 15:55:05 -0800304 << payload_->metadata_size
Gilad Arnoldfe133932014-01-14 12:25:50 -0800305 << ") is missing/incorrect, actual = " << metadata_size_;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700306 *error = ErrorCode::kDownloadInvalidMetadataSize;
Sen Jiang9c89e842018-02-02 13:51:21 -0800307 return MetadataParseResult::kError;
Gilad Arnoldfe133932014-01-14 12:25:50 -0800308 }
309 }
Andrew3a7dc262019-12-19 11:38:08 -0800310
311 // Check that the |metadata signature size_| and |metadata_size_| are not
312 // very big numbers. This is necessary since |update_engine| needs to write
313 // these values into the buffer before being able to use them, and if an
314 // attacker sets these values to a very big number, the buffer will overflow
315 // and |update_engine| will crash. A simple way of solving this is to check
316 // that the size of both values is smaller than the payload itself.
317 if (metadata_size_ + metadata_signature_size_ > payload_->size) {
318 LOG(ERROR) << "The size of the metadata_size(" << metadata_size_ << ")"
319 << " or metadata signature(" << metadata_signature_size_ << ")"
320 << " is greater than the size of the payload"
321 << "(" << payload_->size << ")";
322 *error = ErrorCode::kDownloadInvalidMetadataSize;
323 return MetadataParseResult::kError;
324 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700325 }
326
327 // Now that we have validated the metadata size, we should wait for the full
Sen Jiang76bfa742015-10-12 17:13:26 -0700328 // metadata and its signature (if exist) to be read in before we can parse it.
329 if (payload.size() < metadata_size_ + metadata_signature_size_)
Sen Jiang9c89e842018-02-02 13:51:21 -0800330 return MetadataParseResult::kInsufficientData;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700331
332 // Log whether we validated the size or simply trusting what's in the payload
Jay Srinivasanf4318702012-09-24 11:56:24 -0700333 // here. This is logged here (after we received the full metadata data) so
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700334 // that we just log once (instead of logging n times) if it takes n
335 // DeltaPerformer::Write calls to download the full manifest.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800336 if (payload_->metadata_size == metadata_size_) {
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700337 LOG(INFO) << "Manifest size in payload matches expected value from Omaha";
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800338 } else {
339 // For mandatory-cases, we'd have already returned a kMetadataParseError
340 // above. We'll be here only for non-mandatory cases. Just send a UMA stat.
341 LOG(WARNING) << "Ignoring missing/incorrect metadata size ("
Sen Jiang0affc2c2017-02-10 15:55:05 -0800342 << payload_->metadata_size
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800343 << ") in Omaha response as validation is not mandatory. "
Gilad Arnoldfe133932014-01-14 12:25:50 -0800344 << "Trusting metadata size in payload = " << metadata_size_;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800345 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700346
Amin Hassaniec7bc112020-10-29 16:47:58 -0700347 // NOLINTNEXTLINE(whitespace/braces)
Tianjie Xu7a78d632019-10-08 16:32:39 -0700348 auto [payload_verifier, perform_verification] = CreatePayloadVerifier();
349 if (!payload_verifier) {
350 LOG(ERROR) << "Failed to create payload verifier.";
Sen Jiang08c6da12019-01-07 18:28:56 -0800351 *error = ErrorCode::kDownloadMetadataSignatureVerificationError;
Tianjie Xu6c70b932019-10-22 16:46:00 -0700352 if (perform_verification) {
353 return MetadataParseResult::kError;
354 }
355 } else {
356 // We have the full metadata in |payload|. Verify its integrity
357 // and authenticity based on the information we have in Omaha response.
358 *error = payload_metadata_.ValidateMetadataSignature(
359 payload, payload_->metadata_signature, *payload_verifier);
Sen Jiang08c6da12019-01-07 18:28:56 -0800360 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700361 if (*error != ErrorCode::kSuccess) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800362 if (install_plan_->hash_checks_mandatory) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800363 // The autoupdate_CatchBadSignatures test checks for this string
364 // in log-files. Keep in sync.
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800365 LOG(ERROR) << "Mandatory metadata signature validation failed";
Sen Jiang9c89e842018-02-02 13:51:21 -0800366 return MetadataParseResult::kError;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800367 }
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700368
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800369 // For non-mandatory cases, just send a UMA stat.
370 LOG(WARNING) << "Ignoring metadata signature validation failures";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700371 *error = ErrorCode::kSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700372 }
373
Gilad Arnolddaa27402014-01-23 11:56:17 -0800374 // The payload metadata is deemed valid, it's safe to parse the protobuf.
Sen Jiang9c89e842018-02-02 13:51:21 -0800375 if (!payload_metadata_.GetManifest(payload, &manifest_)) {
Darin Petkov9574f7e2011-01-13 10:48:12 -0800376 LOG(ERROR) << "Unable to parse manifest in update file.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700377 *error = ErrorCode::kDownloadManifestParseError;
Sen Jiang9c89e842018-02-02 13:51:21 -0800378 return MetadataParseResult::kError;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800379 }
Gilad Arnolddaa27402014-01-23 11:56:17 -0800380
381 manifest_parsed_ = true;
Sen Jiang9c89e842018-02-02 13:51:21 -0800382 return MetadataParseResult::kSuccess;
Darin Petkov9574f7e2011-01-13 10:48:12 -0800383}
384
Tianjie8e0090d2021-08-30 22:35:21 -0700385#define OP_DURATION_HISTOGRAM(_op_name, _start_time) \
386 LOCAL_HISTOGRAM_CUSTOM_TIMES( \
387 "UpdateEngine.DownloadAction.InstallOperation::" + string(_op_name) + \
388 ".Duration", \
389 (base::TimeTicks::Now() - _start_time), \
390 base::TimeDelta::FromMilliseconds(10), \
391 base::TimeDelta::FromMinutes(5), \
Amin Hassani008c4582019-01-13 16:22:47 -0800392 20);
Lann Martin39f57142017-07-14 09:18:42 -0600393
Kelvin Zhangcfc531f2022-08-24 17:58:53 +0000394void DeltaPerformer::CheckSPLDowngrade() {
Kelvin Zhanga10fd5a2022-09-01 23:39:39 +0000395 if (!manifest_.has_security_patch_level()) {
396 return;
397 }
Kelvin Zhang862c46c2022-09-02 17:01:12 +0000398 if (manifest_.security_patch_level().empty()) {
399 return;
400 }
Kelvin Zhangcfc531f2022-08-24 17:58:53 +0000401 const auto new_spl = manifest_.security_patch_level();
402 const auto current_spl =
403 android::base::GetProperty("ro.build.version.security_patch", "");
404 if (current_spl.empty()) {
405 LOG(ERROR) << "Failed to get ro.build.version.security_patch, unable to "
406 "determine if this OTA is a SPL downgrade.";
407 return;
408 }
409 if (new_spl < current_spl) {
410 install_plan_->powerwash_required = true;
411 LOG(INFO) << "Target build SPL " << new_spl
412 << " is older than current build's SPL " << current_spl
413 << ", this OTA is an SPL downgrade. Data wipe will be required";
414 }
415}
416
Don Garrette410e0f2011-11-10 15:39:01 -0800417// Wrapper around write. Returns true if all requested bytes
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800418// were written, or false on any error, regardless of progress
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700419// and stores an action exit code in |error|.
Amin Hassani008c4582019-01-13 16:22:47 -0800420bool DeltaPerformer::Write(const void* bytes, size_t count, ErrorCode* error) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700421 *error = ErrorCode::kSuccess;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700422 const char* c_bytes = reinterpret_cast<const char*>(bytes);
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800423
424 // Update the total byte downloaded count and the progress logs.
425 total_bytes_received_ += count;
426 UpdateOverallProgress(false, "Completed ");
427
Gilad Arnoldfe133932014-01-14 12:25:50 -0800428 while (!manifest_valid_) {
Sen Jiangb8060e42015-09-24 17:30:50 -0700429 // Read data up to the needed limit; this is either maximium payload header
430 // size, or the full metadata size (once it becomes known).
431 const bool do_read_header = !IsHeaderParsed();
Amin Hassani008c4582019-01-13 16:22:47 -0800432 CopyDataToBuffer(
433 &c_bytes,
434 &count,
435 (do_read_header ? kMaxPayloadHeaderSize
436 : metadata_size_ + metadata_signature_size_));
Gilad Arnoldfe133932014-01-14 12:25:50 -0800437
Gilad Arnolddaa27402014-01-23 11:56:17 -0800438 MetadataParseResult result = ParsePayloadMetadata(buffer_, error);
Sen Jiang9c89e842018-02-02 13:51:21 -0800439 if (result == MetadataParseResult::kError)
Don Garrette410e0f2011-11-10 15:39:01 -0800440 return false;
Sen Jiang9c89e842018-02-02 13:51:21 -0800441 if (result == MetadataParseResult::kInsufficientData) {
Gilad Arnoldfe133932014-01-14 12:25:50 -0800442 // If we just processed the header, make an attempt on the manifest.
Sen Jiangb8060e42015-09-24 17:30:50 -0700443 if (do_read_header && IsHeaderParsed())
Gilad Arnoldfe133932014-01-14 12:25:50 -0800444 continue;
445
Don Garrette410e0f2011-11-10 15:39:01 -0800446 return true;
Gilad Arnoldfe133932014-01-14 12:25:50 -0800447 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700448
449 // Checks the integrity of the payload manifest.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700450 if ((*error = ValidateManifest()) != ErrorCode::kSuccess)
Gilad Arnold21504f02013-05-24 08:51:22 -0700451 return false;
Gilad Arnolddaa27402014-01-23 11:56:17 -0800452 manifest_valid_ = true;
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400453 if (!install_plan_->is_resume) {
Kelvin Zhangcf4600e2020-10-27 15:50:33 -0400454 auto begin = reinterpret_cast<const char*>(buffer_.data());
455 prefs_->SetString(kPrefsManifestBytes, {begin, buffer_.size()});
Kelvin Zhangcc011d32020-07-10 18:20:08 -0400456 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700457
Gilad Arnoldfe133932014-01-14 12:25:50 -0800458 // Clear the download buffer.
Sen Jiangf6813802015-11-03 21:27:29 -0800459 DiscardBuffer(false, metadata_size_);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700460
Sen Jiang57f91802017-11-14 17:42:13 -0800461 block_size_ = manifest_.block_size();
462
Kelvin Zhangcfc531f2022-08-24 17:58:53 +0000463 CheckSPLDowngrade();
464
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700465 // This populates |partitions_| and the |install_plan.partitions| with the
466 // list of partitions from the manifest.
467 if (!ParseManifestPartitions(error))
468 return false;
469
Sen Jiang5ae865b2017-04-18 14:24:40 -0700470 // |install_plan.partitions| was filled in, nothing need to be done here if
471 // the payload was already applied, returns false to terminate http fetcher,
472 // but keep |error| as ErrorCode::kSuccess.
473 if (payload_->already_applied)
474 return false;
475
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700476 num_total_operations_ = 0;
477 for (const auto& partition : partitions_) {
478 num_total_operations_ += partition.operations_size();
479 acc_num_operations_.push_back(num_total_operations_);
480 }
481
Amin Hassani008c4582019-01-13 16:22:47 -0800482 LOG_IF(WARNING,
483 !prefs_->SetInt64(kPrefsManifestMetadataSize, metadata_size_))
Darin Petkov73058b42010-10-06 16:32:19 -0700484 << "Unable to save the manifest metadata size.";
Amin Hassani008c4582019-01-13 16:22:47 -0800485 LOG_IF(WARNING,
486 !prefs_->SetInt64(kPrefsManifestSignatureSize,
487 metadata_signature_size_))
Alex Deymof25eb492016-02-26 00:20:08 -0800488 << "Unable to save the manifest signature size.";
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700489
Darin Petkov9b230572010-10-08 10:20:09 -0700490 if (!PrimeUpdateState()) {
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700491 *error = ErrorCode::kDownloadStateInitializationError;
Darin Petkov9b230572010-10-08 10:20:09 -0700492 LOG(ERROR) << "Unable to prime the update state.";
Don Garrette410e0f2011-11-10 15:39:01 -0800493 return false;
Darin Petkov9b230572010-10-08 10:20:09 -0700494 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800495
Yifan Hong537802d2018-08-15 13:15:42 -0700496 if (next_operation_num_ < acc_num_operations_[current_partition_]) {
497 if (!OpenCurrentPartition()) {
498 *error = ErrorCode::kInstallDeviceOpenError;
499 return false;
500 }
Allie Woodfdf00512015-03-02 13:34:55 -0800501 }
502
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800503 if (next_operation_num_ > 0)
504 UpdateOverallProgress(true, "Resuming after ");
505 LOG(INFO) << "Starting to apply update payload operations";
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700506 }
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800507
508 while (next_operation_num_ < num_total_operations_) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700509 // Check if we should cancel the current attempt for any reason.
510 // In this case, *error will have already been populated with the reason
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700511 // why we're canceling.
Alex Deymo542c19b2015-12-03 07:43:31 -0300512 if (download_delegate_ && download_delegate_->ShouldCancel(error))
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700513 return false;
514
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700515 // We know there are more operations to perform because we didn't reach the
516 // |num_total_operations_| limit yet.
Yifan Hong537802d2018-08-15 13:15:42 -0700517 if (next_operation_num_ >= acc_num_operations_[current_partition_]) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400518 if (partition_writer_) {
Kelvin Zhang02fe6622021-11-01 16:37:58 -0700519 if (!partition_writer_->FinishedInstallOps()) {
520 *error = ErrorCode::kDownloadWriteError;
521 return false;
522 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400523 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700524 CloseCurrentPartition();
Yifan Hong537802d2018-08-15 13:15:42 -0700525 // Skip until there are operations for current_partition_.
526 while (next_operation_num_ >= acc_num_operations_[current_partition_]) {
527 current_partition_++;
528 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700529 if (!OpenCurrentPartition()) {
530 *error = ErrorCode::kInstallDeviceOpenError;
531 return false;
532 }
533 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700534
Alex Deymoa12ee112015-08-12 22:19:32 -0700535 const InstallOperation& op =
Kelvin Zhang52cb1d72020-10-27 13:44:25 -0400536 partitions_[current_partition_].operations(GetPartitionOperationNum());
Gilad Arnoldfe133932014-01-14 12:25:50 -0800537
538 CopyDataToBuffer(&c_bytes, &count, op.data_length());
539
540 // Check whether we received all of the next operation's data payload.
541 if (!CanPerformInstallOperation(op))
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700542 return true;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700543
Tianjie1205ea62020-07-09 17:04:28 -0700544 // Validate the operation unconditionally. This helps prevent the
545 // exploitation of vulnerabilities in the patching libraries, e.g. bspatch.
546 // The hash of the patch data for a given operation is embedded in the
547 // payload metadata; and thus has been verified against the public key on
548 // device.
549 // Note: Validate must be called only if CanPerformInstallOperation is
550 // called. Otherwise, we might be failing operations before even if there
551 // isn't sufficient data to compute the proper hash.
552 *error = ValidateOperationHash(op);
553 if (*error != ErrorCode::kSuccess) {
554 if (install_plan_->hash_checks_mandatory) {
555 LOG(ERROR) << "Mandatory operation hash check failed";
556 return false;
Jay Srinivasan00f76b62012-09-17 18:48:36 -0700557 }
Tianjie1205ea62020-07-09 17:04:28 -0700558
559 // For non-mandatory cases, just send a UMA stat.
560 LOG(WARNING) << "Ignoring operation validation errors";
561 *error = ErrorCode::kSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -0700562 }
563
Darin Petkov45580e42010-10-08 14:02:40 -0700564 // Makes sure we unblock exit when this operation completes.
Darin Petkov9c0baf82010-10-07 13:44:48 -0700565 ScopedTerminatorExitUnblocker exit_unblocker =
566 ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug.
Gilad Arnoldfe133932014-01-14 12:25:50 -0800567
Lann Martin39f57142017-07-14 09:18:42 -0600568 base::TimeTicks op_start_time = base::TimeTicks::Now();
569
Gilad Arnoldfe133932014-01-14 12:25:50 -0800570 bool op_result;
Tianjie8e0090d2021-08-30 22:35:21 -0700571 const string op_name = InstallOperationTypeName(op.type());
Alex Deymo2d621a32015-10-01 11:09:01 -0700572 switch (op.type()) {
573 case InstallOperation::REPLACE:
574 case InstallOperation::REPLACE_BZ:
575 case InstallOperation::REPLACE_XZ:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700576 op_result = PerformReplaceOperation(op);
Lann Martin39f57142017-07-14 09:18:42 -0600577 OP_DURATION_HISTOGRAM("REPLACE", op_start_time);
Alex Deymo2d621a32015-10-01 11:09:01 -0700578 break;
Alex Deymo79715ad2015-10-02 14:27:53 -0700579 case InstallOperation::ZERO:
580 case InstallOperation::DISCARD:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700581 op_result = PerformZeroOrDiscardOperation(op);
Lann Martin39f57142017-07-14 09:18:42 -0600582 OP_DURATION_HISTOGRAM("ZERO_OR_DISCARD", op_start_time);
Alex Deymo79715ad2015-10-02 14:27:53 -0700583 break;
Alex Deymo2d621a32015-10-01 11:09:01 -0700584 case InstallOperation::SOURCE_COPY:
Sen Jiangbe2c47b2016-06-15 14:09:27 -0700585 op_result = PerformSourceCopyOperation(op, error);
Lann Martin39f57142017-07-14 09:18:42 -0600586 OP_DURATION_HISTOGRAM("SOURCE_COPY", op_start_time);
Alex Deymo2d621a32015-10-01 11:09:01 -0700587 break;
588 case InstallOperation::SOURCE_BSDIFF:
Amin Hassaniefa62d92017-11-09 13:46:56 -0800589 case InstallOperation::BROTLI_BSDIFF:
Amin Hassani49fdb092017-08-04 13:10:59 -0700590 case InstallOperation::PUFFDIFF:
Tianjie8e0090d2021-08-30 22:35:21 -0700591 case InstallOperation::ZUCCHINI:
Kelvin Zhangcb2dc882021-11-22 09:26:50 -0800592 case InstallOperation::LZ4DIFF_PUFFDIFF:
593 case InstallOperation::LZ4DIFF_BSDIFF:
Tianjie8e0090d2021-08-30 22:35:21 -0700594 op_result = PerformDiffOperation(op, error);
595 OP_DURATION_HISTOGRAM(op_name, op_start_time);
Sen Jiangbc3e6b02016-01-19 18:39:26 +0800596 break;
Alex Deymo2d621a32015-10-01 11:09:01 -0700597 default:
Alex Deymoeecb0a52017-05-19 15:15:08 -0700598 op_result = false;
Alex Deymo2d621a32015-10-01 11:09:01 -0700599 }
Tianjie8e0090d2021-08-30 22:35:21 -0700600 if (!HandleOpResult(op_result, op_name.c_str(), error))
Gilad Arnoldfe133932014-01-14 12:25:50 -0800601 return false;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800602
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700603 next_operation_num_++;
Gilad Arnold8a86fa52013-01-15 12:35:05 -0800604 UpdateOverallProgress(false, "Completed ");
Sen Jiang3a25dc22019-01-10 14:14:41 -0800605 CheckpointUpdateProgress(false);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700606 }
Sen Jiangf6813802015-11-03 21:27:29 -0800607
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500608 if (partition_writer_) {
609 TEST_AND_RETURN_FALSE(partition_writer_->FinishedInstallOps());
610 }
611 CloseCurrentPartition();
612
Saint Chou0a92a622020-07-29 14:25:35 +0000613 // In major version 2, we don't add unused operation to the payload.
Alex Deymof25eb492016-02-26 00:20:08 -0800614 // If we already extracted the signature we should skip this step.
Amin Hassani822d4852020-03-10 01:50:42 +0000615 if (manifest_.has_signatures_offset() && manifest_.has_signatures_size() &&
Alex Deymof25eb492016-02-26 00:20:08 -0800616 signatures_message_data_.empty()) {
Sen Jiangf6813802015-11-03 21:27:29 -0800617 if (manifest_.signatures_offset() != buffer_offset_) {
618 LOG(ERROR) << "Payload signatures offset points to blob offset "
619 << manifest_.signatures_offset()
Amin Hassani008c4582019-01-13 16:22:47 -0800620 << " but signatures are expected at offset " << buffer_offset_;
Sen Jiangf6813802015-11-03 21:27:29 -0800621 *error = ErrorCode::kDownloadPayloadVerificationError;
622 return false;
623 }
624 CopyDataToBuffer(&c_bytes, &count, manifest_.signatures_size());
625 // Needs more data to cover entire signature.
626 if (buffer_.size() < manifest_.signatures_size())
627 return true;
628 if (!ExtractSignatureMessage()) {
629 LOG(ERROR) << "Extract payload signature failed.";
630 *error = ErrorCode::kDownloadPayloadVerificationError;
631 return false;
632 }
633 DiscardBuffer(true, 0);
Alex Deymof25eb492016-02-26 00:20:08 -0800634 // Since we extracted the SignatureMessage we need to advance the
635 // checkpoint, otherwise we would reload the signature and try to extract
636 // it again.
Sen Jiang3a25dc22019-01-10 14:14:41 -0800637 // This is the last checkpoint for an update, force this checkpoint to be
638 // saved.
639 CheckpointUpdateProgress(true);
Sen Jiangf6813802015-11-03 21:27:29 -0800640 }
641
Don Garrette410e0f2011-11-10 15:39:01 -0800642 return true;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700643}
644
David Zeuthen8f191b22013-08-06 12:27:50 -0700645bool DeltaPerformer::IsManifestValid() {
646 return manifest_valid_;
647}
648
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700649bool DeltaPerformer::ParseManifestPartitions(ErrorCode* error) {
Kelvin Zhang20982a52021-08-13 12:31:16 -0700650 partitions_.assign(manifest_.partitions().begin(),
651 manifest_.partitions().end());
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700652
Tianjied3865d12020-06-03 15:25:17 -0700653 // For VAB and partial updates, the partition preparation will copy the
654 // dynamic partitions metadata to the target metadata slot, and rename the
655 // slot suffix of the partitions in the metadata.
656 if (install_plan_->target_slot != BootControlInterface::kInvalidSlot) {
657 uint64_t required_size = 0;
658 if (!PreparePartitionsForUpdate(&required_size)) {
659 if (required_size > 0) {
660 *error = ErrorCode::kNotEnoughSpace;
661 } else {
662 *error = ErrorCode::kInstallDeviceOpenError;
663 }
664 return false;
665 }
666 }
667
Tianjie55abd3c2020-06-19 00:22:59 -0700668 // Partitions in manifest are no longer needed after preparing partitions.
Amin Hassani822d4852020-03-10 01:50:42 +0000669 manifest_.clear_partitions();
Tianjied3865d12020-06-03 15:25:17 -0700670 // TODO(xunchang) TBD: allow partial update only on devices with dynamic
671 // partition.
672 if (manifest_.partial_update()) {
673 std::set<std::string> touched_partitions;
674 for (const auto& partition_update : partitions_) {
675 touched_partitions.insert(partition_update.partition_name());
676 }
677
Tianjie99d570d2020-06-04 14:57:19 -0700678 auto generator = partition_update_generator::Create(boot_control_,
679 manifest_.block_size());
Tianjie24f96092020-06-30 12:26:25 -0700680 std::vector<PartitionUpdate> untouched_static_partitions;
Tianjied3865d12020-06-03 15:25:17 -0700681 TEST_AND_RETURN_FALSE(
682 generator->GenerateOperationsForPartitionsNotInPayload(
683 install_plan_->source_slot,
684 install_plan_->target_slot,
685 touched_partitions,
Tianjie24f96092020-06-30 12:26:25 -0700686 &untouched_static_partitions));
687 partitions_.insert(partitions_.end(),
688 untouched_static_partitions.begin(),
689 untouched_static_partitions.end());
690
691 // Save the untouched dynamic partitions in install plan.
692 std::vector<std::string> dynamic_partitions;
693 if (!boot_control_->GetDynamicPartitionControl()
694 ->ListDynamicPartitionsForSlot(install_plan_->source_slot,
Tianjie3a55fc22021-02-13 16:02:22 -0800695 boot_control_->GetCurrentSlot(),
Tianjie24f96092020-06-30 12:26:25 -0700696 &dynamic_partitions)) {
697 LOG(ERROR) << "Failed to load dynamic partitions from slot "
698 << install_plan_->source_slot;
699 return false;
700 }
701 install_plan_->untouched_dynamic_partitions.clear();
702 for (const auto& name : dynamic_partitions) {
703 if (touched_partitions.find(name) == touched_partitions.end()) {
704 install_plan_->untouched_dynamic_partitions.push_back(name);
705 }
706 }
Tianjied3865d12020-06-03 15:25:17 -0700707 }
708
Kelvin Zhang20982a52021-08-13 12:31:16 -0700709 if (!install_plan_->ParsePartitions(
710 partitions_, boot_control_, block_size_, error)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700711 return false;
712 }
Kelvin Zhang20982a52021-08-13 12:31:16 -0700713
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700714 LogPartitionInfo(partitions_);
715 return true;
716}
717
Yifan Hongb9d63572020-01-09 17:50:46 -0800718bool DeltaPerformer::PreparePartitionsForUpdate(uint64_t* required_size) {
719 // Call static PreparePartitionsForUpdate with hash from
720 // kPrefsUpdateCheckResponseHash to ensure hash of payload that space is
721 // preallocated for is the same as the hash of payload being applied.
722 string update_check_response_hash;
723 ignore_result(prefs_->GetString(kPrefsUpdateCheckResponseHash,
724 &update_check_response_hash));
725 return PreparePartitionsForUpdate(prefs_,
726 boot_control_,
727 install_plan_->target_slot,
728 manifest_,
729 update_check_response_hash,
730 required_size);
731}
732
733bool DeltaPerformer::PreparePartitionsForUpdate(
734 PrefsInterface* prefs,
735 BootControlInterface* boot_control,
736 BootControlInterface::Slot target_slot,
737 const DeltaArchiveManifest& manifest,
738 const std::string& update_check_response_hash,
739 uint64_t* required_size) {
740 string last_hash;
741 ignore_result(
742 prefs->GetString(kPrefsDynamicPartitionMetadataUpdated, &last_hash));
743
744 bool is_resume = !update_check_response_hash.empty() &&
745 last_hash == update_check_response_hash;
746
747 if (is_resume) {
748 LOG(INFO) << "Using previously prepared partitions for update. hash = "
749 << last_hash;
750 } else {
751 LOG(INFO) << "Preparing partitions for new update. last hash = "
752 << last_hash << ", new hash = " << update_check_response_hash;
Kelvin Zhang51e08b92021-02-19 14:54:21 -0500753 ResetUpdateProgress(prefs, false);
Yifan Hongb9d63572020-01-09 17:50:46 -0800754 }
755
756 if (!boot_control->GetDynamicPartitionControl()->PreparePartitionsForUpdate(
757 boot_control->GetCurrentSlot(),
758 target_slot,
759 manifest,
760 !is_resume /* should update */,
761 required_size)) {
Yifan Hong9acd9cb2018-10-19 14:52:45 -0700762 LOG(ERROR) << "Unable to initialize partition metadata for slot "
Yifan Hongb9d63572020-01-09 17:50:46 -0800763 << BootControlInterface::SlotName(target_slot);
Yifan Hong9acd9cb2018-10-19 14:52:45 -0700764 return false;
765 }
Yifan Hongb9d63572020-01-09 17:50:46 -0800766
767 TEST_AND_RETURN_FALSE(prefs->SetString(kPrefsDynamicPartitionMetadataUpdated,
768 update_check_response_hash));
Yifan Hong13d41cb2019-09-16 13:18:22 -0700769 LOG(INFO) << "PreparePartitionsForUpdate done.";
Yifan Hong9acd9cb2018-10-19 14:52:45 -0700770
771 return true;
772}
773
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700774bool DeltaPerformer::CanPerformInstallOperation(
Alex Deymoa12ee112015-08-12 22:19:32 -0700775 const chromeos_update_engine::InstallOperation& operation) {
Alex Deymo0497d052016-03-23 09:16:59 -0700776 // If we don't have a data blob we can apply it right away.
777 if (!operation.has_data_offset() && !operation.has_data_length())
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700778 return true;
779
780 // See if we have the entire data blob in the buffer
781 if (operation.data_offset() < buffer_offset_) {
782 LOG(ERROR) << "we threw away data it seems?";
783 return false;
784 }
Darin Petkovd7061ab2010-10-06 14:37:09 -0700785
Gilad Arnoldfe133932014-01-14 12:25:50 -0800786 return (operation.data_offset() + operation.data_length() <=
787 buffer_offset_ + buffer_.size());
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700788}
789
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700790bool DeltaPerformer::PerformReplaceOperation(
791 const InstallOperation& operation) {
Alex Deymoa12ee112015-08-12 22:19:32 -0700792 CHECK(operation.type() == InstallOperation::REPLACE ||
Alex Deymo2d621a32015-10-01 11:09:01 -0700793 operation.type() == InstallOperation::REPLACE_BZ ||
794 operation.type() == InstallOperation::REPLACE_XZ);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700795
796 // Since we delete data off the beginning of the buffer as we use it,
797 // the data we need should be exactly at the beginning of the buffer.
Darin Petkov9b230572010-10-08 10:20:09 -0700798 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
Darin Petkovd7061ab2010-10-06 14:37:09 -0700799
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400800 TEST_AND_RETURN_FALSE(partition_writer_->PerformReplaceOperation(
801 operation, buffer_.data(), buffer_.size()));
802 // Update buffer
803 DiscardBuffer(true, buffer_.size());
804 return true;
805}
806
Alex Deymo79715ad2015-10-02 14:27:53 -0700807bool DeltaPerformer::PerformZeroOrDiscardOperation(
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700808 const InstallOperation& operation) {
Alex Deymo79715ad2015-10-02 14:27:53 -0700809 CHECK(operation.type() == InstallOperation::DISCARD ||
810 operation.type() == InstallOperation::ZERO);
811
812 // These operations have no blob.
813 TEST_AND_RETURN_FALSE(!operation.has_data_offset());
814 TEST_AND_RETURN_FALSE(!operation.has_data_length());
Kelvin Zhang50bac652020-09-28 15:51:41 -0400815
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400816 return partition_writer_->PerformZeroOrDiscardOperation(operation);
817}
Alex Deymo79715ad2015-10-02 14:27:53 -0700818
Allie Wood9f6f0a52015-03-30 11:25:47 -0700819bool DeltaPerformer::PerformSourceCopyOperation(
Sen Jiangbe2c47b2016-06-15 14:09:27 -0700820 const InstallOperation& operation, ErrorCode* error) {
Allie Wood9f6f0a52015-03-30 11:25:47 -0700821 if (operation.has_src_length())
822 TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0);
823 if (operation.has_dst_length())
824 TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0);
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400825 return partition_writer_->PerformSourceCopyOperation(operation, error);
826}
Allie Wood9f6f0a52015-03-30 11:25:47 -0700827
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700828bool DeltaPerformer::ExtentsToBsdiffPositionsString(
829 const RepeatedPtrField<Extent>& extents,
830 uint64_t block_size,
831 uint64_t full_length,
832 string* positions_string) {
833 string ret;
834 uint64_t length = 0;
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -0700835 for (const Extent& extent : extents) {
Allie Wood56873452015-03-27 17:48:40 -0700836 int64_t start = extent.start_block() * block_size;
Tamas Berghammer9d66d762016-07-15 14:19:04 +0100837 uint64_t this_length =
838 min(full_length - length,
839 static_cast<uint64_t>(extent.num_blocks()) * block_size);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700840 ret += base::StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700841 length += this_length;
842 }
843 TEST_AND_RETURN_FALSE(length == full_length);
844 if (!ret.empty())
845 ret.resize(ret.size() - 1); // Strip trailing comma off
846 *positions_string = ret;
847 return true;
848}
849
Tianjie8e0090d2021-08-30 22:35:21 -0700850bool DeltaPerformer::PerformDiffOperation(const InstallOperation& operation,
851 ErrorCode* error) {
Allie Wood9f6f0a52015-03-30 11:25:47 -0700852 // Since we delete data off the beginning of the buffer as we use it,
853 // the data we need should be exactly at the beginning of the buffer.
854 TEST_AND_RETURN_FALSE(buffer_offset_ == operation.data_offset());
855 TEST_AND_RETURN_FALSE(buffer_.size() >= operation.data_length());
856 if (operation.has_src_length())
857 TEST_AND_RETURN_FALSE(operation.src_length() % block_size_ == 0);
858 if (operation.has_dst_length())
859 TEST_AND_RETURN_FALSE(operation.dst_length() % block_size_ == 0);
860
Tianjie8e0090d2021-08-30 22:35:21 -0700861 TEST_AND_RETURN_FALSE(partition_writer_->PerformDiffOperation(
Kelvin Zhang9bd519d2020-09-23 12:55:19 -0400862 operation, error, buffer_.data(), buffer_.size()));
863 DiscardBuffer(true, buffer_.size());
864 return true;
865}
Amin Hassani02855c22017-09-06 22:34:50 -0700866
Sen Jiangf6813802015-11-03 21:27:29 -0800867bool DeltaPerformer::ExtractSignatureMessage() {
Darin Petkovd7061ab2010-10-06 14:37:09 -0700868 TEST_AND_RETURN_FALSE(signatures_message_data_.empty());
869 TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset());
870 TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size());
Darin Petkov4f0a07b2011-05-25 16:47:20 -0700871 signatures_message_data_.assign(
Amin Hassani008c4582019-01-13 16:22:47 -0800872 buffer_.begin(), buffer_.begin() + manifest_.signatures_size());
Darin Petkov4f0a07b2011-05-25 16:47:20 -0700873
Darin Petkovd7061ab2010-10-06 14:37:09 -0700874 LOG(INFO) << "Extracted signature data of size "
875 << manifest_.signatures_size() << " at "
876 << manifest_.signatures_offset();
877 return true;
878}
879
Sen Jiang08c6da12019-01-07 18:28:56 -0800880bool DeltaPerformer::GetPublicKey(string* out_public_key) {
881 out_public_key->clear();
David Zeuthene7f89172013-10-31 10:21:04 -0700882
Sen Jiang08c6da12019-01-07 18:28:56 -0800883 if (utils::FileExists(public_key_path_.c_str())) {
884 LOG(INFO) << "Verifying using public key: " << public_key_path_;
885 return utils::ReadFile(public_key_path_, out_public_key);
886 }
887
Kelvin Zhang50bac652020-09-28 15:51:41 -0400888 // If this is an official build then we are not allowed to use public key
889 // from Omaha response.
Sen Jiang08c6da12019-01-07 18:28:56 -0800890 if (!hardware_->IsOfficialBuild() && !install_plan_->public_key_rsa.empty()) {
891 LOG(INFO) << "Verifying using public key from Omaha response.";
892 return brillo::data_encoding::Base64Decode(install_plan_->public_key_rsa,
893 out_public_key);
894 }
Tianjie Xu7a78d632019-10-08 16:32:39 -0700895 LOG(INFO) << "No public keys found for verification.";
David Zeuthene7f89172013-10-31 10:21:04 -0700896 return true;
897}
898
Tianjie Xu7a78d632019-10-08 16:32:39 -0700899std::pair<std::unique_ptr<PayloadVerifier>, bool>
900DeltaPerformer::CreatePayloadVerifier() {
901 if (utils::FileExists(update_certificates_path_.c_str())) {
902 LOG(INFO) << "Verifying using certificates: " << update_certificates_path_;
903 return {
904 PayloadVerifier::CreateInstanceFromZipPath(update_certificates_path_),
905 true};
906 }
907
908 string public_key;
909 if (!GetPublicKey(&public_key)) {
910 LOG(ERROR) << "Failed to read public key";
911 return {nullptr, true};
912 }
913
914 // Skips the verification if the public key is empty.
915 if (public_key.empty()) {
916 return {nullptr, false};
917 }
Kelvin Zhang3fe49642021-10-04 15:35:02 -0700918 LOG(INFO) << "Verifing using public key: " << public_key;
Tianjie Xu7a78d632019-10-08 16:32:39 -0700919 return {PayloadVerifier::CreateInstance(public_key), true};
920}
921
Gilad Arnold21504f02013-05-24 08:51:22 -0700922ErrorCode DeltaPerformer::ValidateManifest() {
Saint Chou0a92a622020-07-29 14:25:35 +0000923 // Perform assorted checks to validation check the manifest, make sure it
Don Garrettb8dd1d92013-11-22 17:40:02 -0800924 // matches data from other sources, and that it is a supported version.
Amin Hassani822d4852020-03-10 01:50:42 +0000925 bool has_old_fields = std::any_of(manifest_.partitions().begin(),
926 manifest_.partitions().end(),
927 [](const PartitionUpdate& partition) {
928 return partition.has_old_partition_info();
929 });
Sen Jiangc8f6b7a2015-10-21 11:09:59 -0700930
Alex Deymo64d98782016-02-05 18:03:48 -0800931 // The presence of an old partition hash is the sole indicator for a delta
Tianjief5baff42020-07-17 21:43:22 -0700932 // update. Also, always treat the partial update as delta so that we can
933 // perform the minor version check correctly.
Alex Deymo64d98782016-02-05 18:03:48 -0800934 InstallPayloadType actual_payload_type =
Tianjief5baff42020-07-17 21:43:22 -0700935 (has_old_fields || manifest_.partial_update())
936 ? InstallPayloadType::kDelta
937 : InstallPayloadType::kFull;
Alex Deymo64d98782016-02-05 18:03:48 -0800938
Sen Jiangcdd52062017-05-18 15:33:10 -0700939 if (payload_->type == InstallPayloadType::kUnknown) {
Alex Deymo64d98782016-02-05 18:03:48 -0800940 LOG(INFO) << "Detected a '"
941 << InstallPayloadTypeToString(actual_payload_type)
942 << "' payload.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700943 payload_->type = actual_payload_type;
944 } else if (payload_->type != actual_payload_type) {
Alex Deymo64d98782016-02-05 18:03:48 -0800945 LOG(ERROR) << "InstallPlan expected a '"
Sen Jiangcdd52062017-05-18 15:33:10 -0700946 << InstallPayloadTypeToString(payload_->type)
Alex Deymo64d98782016-02-05 18:03:48 -0800947 << "' payload but the downloaded manifest contains a '"
948 << InstallPayloadTypeToString(actual_payload_type)
949 << "' payload.";
950 return ErrorCode::kPayloadMismatchedType;
951 }
Alex Deymo64d98782016-02-05 18:03:48 -0800952 // Check that the minor version is compatible.
Tianjied3865d12020-06-03 15:25:17 -0700953 // TODO(xunchang) increment minor version & add check for partial update
Alex Deymo64d98782016-02-05 18:03:48 -0800954 if (actual_payload_type == InstallPayloadType::kFull) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800955 if (manifest_.minor_version() != kFullPayloadMinorVersion) {
956 LOG(ERROR) << "Manifest contains minor version "
957 << manifest_.minor_version()
958 << ", but all full payloads should have version "
959 << kFullPayloadMinorVersion << ".";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700960 return ErrorCode::kUnsupportedMinorPayloadVersion;
Don Garrettb8dd1d92013-11-22 17:40:02 -0800961 }
962 } else {
Sen Jiangf1236632018-05-11 16:03:23 -0700963 if (manifest_.minor_version() < kMinSupportedMinorPayloadVersion ||
964 manifest_.minor_version() > kMaxSupportedMinorPayloadVersion) {
Don Garrettb8dd1d92013-11-22 17:40:02 -0800965 LOG(ERROR) << "Manifest contains minor version "
966 << manifest_.minor_version()
Sen Jiangf1236632018-05-11 16:03:23 -0700967 << " not in the range of supported minor versions ["
968 << kMinSupportedMinorPayloadVersion << ", "
969 << kMaxSupportedMinorPayloadVersion << "].";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700970 return ErrorCode::kUnsupportedMinorPayloadVersion;
Don Garrettb8dd1d92013-11-22 17:40:02 -0800971 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700972 }
973
Yifan Hong87029332020-09-01 17:20:08 -0700974 ErrorCode error_code = CheckTimestampError();
975 if (error_code != ErrorCode::kSuccess) {
976 if (error_code == ErrorCode::kPayloadTimestampError) {
977 if (!hardware_->AllowDowngrade()) {
978 return ErrorCode::kPayloadTimestampError;
979 }
980 LOG(INFO) << "The current OS build allows downgrade, continuing to apply"
981 " the payload with an older timestamp.";
982 } else {
983 LOG(ERROR) << "Timestamp check returned "
984 << utils::ErrorCodeToString(error_code);
985 return error_code;
Tianjie Xu4ad3af62019-10-30 11:59:45 -0700986 }
Sen Jiang8e768e92017-06-28 17:13:19 -0700987 }
988
Amin Hassani55c75412019-10-07 11:20:39 -0700989 // TODO(crbug.com/37661) we should be adding more and more manifest checks,
990 // such as partition boundaries, etc.
Gilad Arnold21504f02013-05-24 08:51:22 -0700991
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700992 return ErrorCode::kSuccess;
Gilad Arnold21504f02013-05-24 08:51:22 -0700993}
994
Yifan Hong87029332020-09-01 17:20:08 -0700995ErrorCode DeltaPerformer::CheckTimestampError() const {
Kelvin Zhangd7191032020-08-11 10:48:16 -0400996 bool is_partial_update =
997 manifest_.has_partial_update() && manifest_.partial_update();
998 const auto& partitions = manifest_.partitions();
Yifan Hong87029332020-09-01 17:20:08 -0700999
1000 // Check version field for a given PartitionUpdate object. If an error
1001 // is encountered, set |error_code| accordingly. If downgrade is detected,
Kelvin Zhang50bac652020-09-28 15:51:41 -04001002 // |downgrade_detected| is set. Return true if the program should continue
1003 // to check the next partition or not, or false if it should exit early due
1004 // to errors.
Yifan Hong87029332020-09-01 17:20:08 -07001005 auto&& timestamp_valid = [this](const PartitionUpdate& partition,
1006 bool allow_empty_version,
1007 bool* downgrade_detected) -> ErrorCode {
Tianjie19e55292020-10-19 21:49:37 -07001008 const auto& partition_name = partition.partition_name();
Yifan Hong87029332020-09-01 17:20:08 -07001009 if (!partition.has_version()) {
Tianjie19e55292020-10-19 21:49:37 -07001010 if (hardware_->GetVersionForLogging(partition_name).empty()) {
1011 LOG(INFO) << partition_name << " does't have version, skipping "
1012 << "downgrade check.";
1013 return ErrorCode::kSuccess;
1014 }
1015
Yifan Hong87029332020-09-01 17:20:08 -07001016 if (allow_empty_version) {
1017 return ErrorCode::kSuccess;
1018 }
1019 LOG(ERROR)
Tianjie19e55292020-10-19 21:49:37 -07001020 << "PartitionUpdate " << partition_name
1021 << " doesn't have a version field. Not allowed in partial updates.";
Yifan Hong87029332020-09-01 17:20:08 -07001022 return ErrorCode::kDownloadManifestParseError;
1023 }
1024
Tianjie19e55292020-10-19 21:49:37 -07001025 auto error_code =
1026 hardware_->IsPartitionUpdateValid(partition_name, partition.version());
Yifan Hong87029332020-09-01 17:20:08 -07001027 switch (error_code) {
1028 case ErrorCode::kSuccess:
1029 break;
1030 case ErrorCode::kPayloadTimestampError:
1031 *downgrade_detected = true;
Tianjie19e55292020-10-19 21:49:37 -07001032 LOG(WARNING) << "PartitionUpdate " << partition_name
Yifan Hong87029332020-09-01 17:20:08 -07001033 << " has an older version than partition on device.";
1034 break;
1035 default:
Tianjie19e55292020-10-19 21:49:37 -07001036 LOG(ERROR) << "IsPartitionUpdateValid(" << partition_name
Yifan Hong87029332020-09-01 17:20:08 -07001037 << ") returned" << utils::ErrorCodeToString(error_code);
1038 break;
1039 }
1040 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001041 };
Yifan Hong87029332020-09-01 17:20:08 -07001042
1043 bool downgrade_detected = false;
1044
Kelvin Zhangd7191032020-08-11 10:48:16 -04001045 if (is_partial_update) {
1046 // for partial updates, all partition MUST have valid timestamps
1047 // But max_timestamp can be empty
1048 for (const auto& partition : partitions) {
Yifan Hong87029332020-09-01 17:20:08 -07001049 auto error_code = timestamp_valid(
1050 partition, false /* allow_empty_version */, &downgrade_detected);
1051 if (error_code != ErrorCode::kSuccess &&
1052 error_code != ErrorCode::kPayloadTimestampError) {
1053 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001054 }
1055 }
Yifan Hong87029332020-09-01 17:20:08 -07001056 if (downgrade_detected) {
1057 return ErrorCode::kPayloadTimestampError;
1058 }
1059 return ErrorCode::kSuccess;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001060 }
Yifan Hong87029332020-09-01 17:20:08 -07001061
1062 // For non-partial updates, check max_timestamp first.
Kelvin Zhangd7191032020-08-11 10:48:16 -04001063 if (manifest_.max_timestamp() < hardware_->GetBuildTimestamp()) {
1064 LOG(ERROR) << "The current OS build timestamp ("
1065 << hardware_->GetBuildTimestamp()
1066 << ") is newer than the maximum timestamp in the manifest ("
1067 << manifest_.max_timestamp() << ")";
Yifan Hong87029332020-09-01 17:20:08 -07001068 return ErrorCode::kPayloadTimestampError;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001069 }
1070 // Otherwise... partitions can have empty timestamps.
1071 for (const auto& partition : partitions) {
Yifan Hong87029332020-09-01 17:20:08 -07001072 auto error_code = timestamp_valid(
1073 partition, true /* allow_empty_version */, &downgrade_detected);
1074 if (error_code != ErrorCode::kSuccess &&
1075 error_code != ErrorCode::kPayloadTimestampError) {
1076 return error_code;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001077 }
1078 }
Yifan Hong87029332020-09-01 17:20:08 -07001079 if (downgrade_detected) {
1080 return ErrorCode::kPayloadTimestampError;
1081 }
1082 return ErrorCode::kSuccess;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001083}
1084
David Zeuthena99981f2013-04-29 13:42:47 -07001085ErrorCode DeltaPerformer::ValidateOperationHash(
Alex Deymoa12ee112015-08-12 22:19:32 -07001086 const InstallOperation& operation) {
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001087 if (!operation.data_sha256_hash().size()) {
1088 if (!operation.data_length()) {
Kelvin Zhang50bac652020-09-28 15:51:41 -04001089 // Operations that do not have any data blob won't have any operation
1090 // hash either. So, these operations are always considered validated
1091 // since the metadata that contains all the non-data-blob portions of
1092 // the operation has already been validated. This is true for both HTTP
1093 // and HTTPS cases.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001094 return ErrorCode::kSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001095 }
1096
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001097 // No hash is present for an operation that has data blobs. This shouldn't
1098 // happen normally for any client that has this code, because the
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001099 // corresponding update should have been produced with the operation
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001100 // hashes. So if it happens it means either we've turned operation hash
1101 // generation off in DeltaDiffGenerator or it's a regression of some sort.
Saint Chou0a92a622020-07-29 14:25:35 +00001102 // One caveat though: The last operation is a unused signature operation
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001103 // that doesn't have a hash at the time the manifest is created. So we
1104 // should not complaint about that operation. This operation can be
1105 // recognized by the fact that it's offset is mentioned in the manifest.
1106 if (manifest_.signatures_offset() &&
1107 manifest_.signatures_offset() == operation.data_offset()) {
1108 LOG(INFO) << "Skipping hash verification for signature operation "
1109 << next_operation_num_ + 1;
1110 } else {
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001111 if (install_plan_->hash_checks_mandatory) {
1112 LOG(ERROR) << "Missing mandatory operation hash for operation "
1113 << next_operation_num_ + 1;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001114 return ErrorCode::kDownloadOperationHashMissingError;
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001115 }
1116
Jay Srinivasan738fdf32012-12-07 17:40:54 -08001117 LOG(WARNING) << "Cannot validate operation " << next_operation_num_ + 1
1118 << " as there's no operation hash in manifest";
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001119 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001120 return ErrorCode::kSuccess;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001121 }
1122
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001123 brillo::Blob expected_op_hash;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001124 expected_op_hash.assign(operation.data_sha256_hash().data(),
1125 (operation.data_sha256_hash().data() +
1126 operation.data_sha256_hash().size()));
1127
Sen Jiang2703ef42017-03-16 13:36:21 -07001128 brillo::Blob calculated_op_hash;
1129 if (!HashCalculator::RawHashOfBytes(
1130 buffer_.data(), operation.data_length(), &calculated_op_hash)) {
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001131 LOG(ERROR) << "Unable to compute actual hash of operation "
1132 << next_operation_num_;
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001133 return ErrorCode::kDownloadOperationHashVerificationError;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001134 }
1135
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001136 if (calculated_op_hash != expected_op_hash) {
1137 LOG(ERROR) << "Hash verification failed for operation "
Kelvin Zhangb9368922022-03-17 21:11:32 -07001138 << next_operation_num_
1139 << ". Expected hash = " << HexEncode(expected_op_hash);
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001140 LOG(ERROR) << "Calculated hash over " << operation.data_length()
Kelvin Zhangb9368922022-03-17 21:11:32 -07001141 << " bytes at offset: " << operation.data_offset() << " = "
1142 << HexEncode(calculated_op_hash);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001143 return ErrorCode::kDownloadOperationHashMismatch;
Jay Srinivasan00f76b62012-09-17 18:48:36 -07001144 }
1145
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001146 return ErrorCode::kSuccess;
Jay Srinivasan51dcf262012-09-13 17:24:32 -07001147}
1148
Amin Hassani008c4582019-01-13 16:22:47 -08001149#define TEST_AND_RETURN_VAL(_retval, _condition) \
1150 do { \
1151 if (!(_condition)) { \
1152 LOG(ERROR) << "VerifyPayload failure: " << #_condition; \
1153 return _retval; \
1154 } \
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001155 } while (0);
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001156
David Zeuthena99981f2013-04-29 13:42:47 -07001157ErrorCode DeltaPerformer::VerifyPayload(
Sen Jiang2703ef42017-03-16 13:36:21 -07001158 const brillo::Blob& update_check_response_hash,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001159 const uint64_t update_check_response_size) {
Jay Srinivasan0d8fb402012-05-07 19:19:38 -07001160 // Verifies the download size.
Sen Jiang3a25dc22019-01-10 14:14:41 -08001161 if (update_check_response_size !=
1162 metadata_size_ + metadata_signature_size_ + buffer_offset_) {
1163 LOG(ERROR) << "update_check_response_size (" << update_check_response_size
1164 << ") doesn't match metadata_size (" << metadata_size_
1165 << ") + metadata_signature_size (" << metadata_signature_size_
1166 << ") + buffer_offset (" << buffer_offset_ << ").";
1167 return ErrorCode::kPayloadSizeMismatchError;
1168 }
Jay Srinivasan0d8fb402012-05-07 19:19:38 -07001169
Amin Hassanie331f5a2020-10-06 15:53:29 -07001170 // Verifies the payload hash.
1171 TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadVerificationError,
1172 !payload_hash_calculator_.raw_hash().empty());
Kelvin Zhangdf3c1952021-10-06 18:56:55 -07001173 if (payload_hash_calculator_.raw_hash() != update_check_response_hash) {
1174 LOG(ERROR) << "Actual hash: "
1175 << HexEncode(payload_hash_calculator_.raw_hash())
1176 << ", expected hash: " << HexEncode(update_check_response_hash);
1177 return ErrorCode::kPayloadHashMismatchError;
1178 }
Amin Hassanie331f5a2020-10-06 15:53:29 -07001179
Amin Hassaniec7bc112020-10-29 16:47:58 -07001180 // NOLINTNEXTLINE(whitespace/braces)
Amin Hassanif624e112020-09-15 15:30:08 -07001181 auto [payload_verifier, perform_verification] = CreatePayloadVerifier();
1182 if (!perform_verification) {
1183 LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
1184 return ErrorCode::kSuccess;
1185 }
1186 if (!payload_verifier) {
1187 LOG(ERROR) << "Failed to create the payload verifier.";
1188 return ErrorCode::kDownloadPayloadPubKeyVerificationError;
1189 }
1190
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001191 TEST_AND_RETURN_VAL(ErrorCode::kSignedDeltaPayloadExpectedError,
Andrew de los Reyes771e1bd2011-08-30 14:47:23 -07001192 !signatures_message_data_.empty());
Sen Jiangf6813802015-11-03 21:27:29 -08001193 brillo::Blob hash_data = signed_hash_calculator_.raw_hash();
Alex Deymob552a682015-09-30 09:36:49 -07001194 TEST_AND_RETURN_VAL(ErrorCode::kDownloadPayloadPubKeyVerificationError,
xunchangcda3c032019-03-26 15:41:14 -07001195 hash_data.size() == kSHA256Size);
Alex Deymob552a682015-09-30 09:36:49 -07001196
Tianjie Xu6cf830b2019-09-30 11:31:49 -07001197 if (!payload_verifier->VerifySignature(signatures_message_data_, hash_data)) {
David Zeuthenbc27aac2013-11-26 11:17:48 -08001198 // The autoupdate_CatchBadSignatures test checks for this string
1199 // in log-files. Keep in sync.
Alex Deymob552a682015-09-30 09:36:49 -07001200 LOG(ERROR) << "Public key verification failed, thus update failed.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001201 return ErrorCode::kDownloadPayloadPubKeyVerificationError;
Andrew de los Reyesfb830ba2011-04-04 11:42:43 -07001202 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -08001203
David Zeuthene7f89172013-10-31 10:21:04 -07001204 LOG(INFO) << "Payload hash matches value in payload.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -07001205 return ErrorCode::kSuccess;
Darin Petkovd7061ab2010-10-06 14:37:09 -07001206}
1207
Sen Jiangf6813802015-11-03 21:27:29 -08001208void DeltaPerformer::DiscardBuffer(bool do_advance_offset,
1209 size_t signed_hash_buffer_size) {
Gilad Arnoldfe133932014-01-14 12:25:50 -08001210 // Update the buffer offset.
Gilad Arnolddaa27402014-01-23 11:56:17 -08001211 if (do_advance_offset)
Gilad Arnoldfe133932014-01-14 12:25:50 -08001212 buffer_offset_ += buffer_.size();
1213
1214 // Hash the content.
Sen Jiangf6813802015-11-03 21:27:29 -08001215 payload_hash_calculator_.Update(buffer_.data(), buffer_.size());
1216 signed_hash_calculator_.Update(buffer_.data(), signed_hash_buffer_size);
Gilad Arnoldfe133932014-01-14 12:25:50 -08001217
1218 // Swap content with an empty vector to ensure that all memory is released.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001219 brillo::Blob().swap(buffer_);
Darin Petkovd7061ab2010-10-06 14:37:09 -07001220}
1221
Darin Petkov0406e402010-10-06 21:33:11 -07001222bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs,
Chih-Hung Hsieh5c6bb1d2016-07-27 13:33:15 -07001223 const string& update_check_response_hash) {
Darin Petkov0406e402010-10-06 21:33:11 -07001224 int64_t next_operation = kUpdateStateOperationInvalid;
Alex Deymo3310b222015-03-30 15:59:07 -07001225 if (!(prefs->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) &&
Amin Hassani008c4582019-01-13 16:22:47 -08001226 next_operation != kUpdateStateOperationInvalid && next_operation > 0))
Alex Deymo3310b222015-03-30 15:59:07 -07001227 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001228
1229 string interrupted_hash;
Alex Deymo3310b222015-03-30 15:59:07 -07001230 if (!(prefs->GetString(kPrefsUpdateCheckResponseHash, &interrupted_hash) &&
1231 !interrupted_hash.empty() &&
1232 interrupted_hash == update_check_response_hash))
1233 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001234
Darin Petkov61426142010-10-08 11:04:55 -07001235 int64_t resumed_update_failures;
Kelvin Zhang50bac652020-09-28 15:51:41 -04001236 // Note that storing this value is optional, but if it is there it should
1237 // not be more than the limit.
Alex Deymof25eb492016-02-26 00:20:08 -08001238 if (prefs->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures) &&
1239 resumed_update_failures > kMaxResumedUpdateFailures)
Alex Deymo3310b222015-03-30 15:59:07 -07001240 return false;
Darin Petkov61426142010-10-08 11:04:55 -07001241
Saint Chou0a92a622020-07-29 14:25:35 +00001242 // Validation check the rest.
Darin Petkov0406e402010-10-06 21:33:11 -07001243 int64_t next_data_offset = -1;
Alex Deymo3310b222015-03-30 15:59:07 -07001244 if (!(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) &&
1245 next_data_offset >= 0))
1246 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001247
Darin Petkov437adc42010-10-07 13:12:24 -07001248 string sha256_context;
Alex Deymo3310b222015-03-30 15:59:07 -07001249 if (!(prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) &&
1250 !sha256_context.empty()))
1251 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001252
1253 int64_t manifest_metadata_size = 0;
Alex Deymo3310b222015-03-30 15:59:07 -07001254 if (!(prefs->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) &&
1255 manifest_metadata_size > 0))
1256 return false;
Darin Petkov0406e402010-10-06 21:33:11 -07001257
Alex Deymof25eb492016-02-26 00:20:08 -08001258 int64_t manifest_signature_size = 0;
1259 if (!(prefs->GetInt64(kPrefsManifestSignatureSize,
1260 &manifest_signature_size) &&
1261 manifest_signature_size >= 0))
1262 return false;
1263
Darin Petkov0406e402010-10-06 21:33:11 -07001264 return true;
1265}
1266
Yifan Hong55c2bfe2020-01-13 17:01:19 -08001267bool DeltaPerformer::ResetUpdateProgress(
1268 PrefsInterface* prefs,
1269 bool quick,
1270 bool skip_dynamic_partititon_metadata_updated) {
Darin Petkov0406e402010-10-06 21:33:11 -07001271 TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation,
1272 kUpdateStateOperationInvalid));
Darin Petkov9b230572010-10-08 10:20:09 -07001273 if (!quick) {
Darin Petkov9b230572010-10-08 10:20:09 -07001274 prefs->SetInt64(kPrefsUpdateStateNextDataOffset, -1);
David Zeuthen41996ad2013-09-24 15:43:24 -07001275 prefs->SetInt64(kPrefsUpdateStateNextDataLength, 0);
Darin Petkov9b230572010-10-08 10:20:09 -07001276 prefs->SetString(kPrefsUpdateStateSHA256Context, "");
1277 prefs->SetString(kPrefsUpdateStateSignedSHA256Context, "");
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001278 prefs->SetString(kPrefsUpdateStateSignatureBlob, "");
Darin Petkov9b230572010-10-08 10:20:09 -07001279 prefs->SetInt64(kPrefsManifestMetadataSize, -1);
Alex Deymof25eb492016-02-26 00:20:08 -08001280 prefs->SetInt64(kPrefsManifestSignatureSize, -1);
Darin Petkov61426142010-10-08 11:04:55 -07001281 prefs->SetInt64(kPrefsResumedUpdateFailures, 0);
Sen Jiangfe522822017-10-31 15:14:11 -07001282 prefs->Delete(kPrefsPostInstallSucceeded);
Sen Jiang3eeaf7d2018-10-11 13:55:32 -07001283 prefs->Delete(kPrefsVerityWritten);
Yifan Hong55c2bfe2020-01-13 17:01:19 -08001284
1285 if (!skip_dynamic_partititon_metadata_updated) {
1286 LOG(INFO) << "Resetting recorded hash for prepared partitions.";
1287 prefs->Delete(kPrefsDynamicPartitionMetadataUpdated);
1288 }
Darin Petkov9b230572010-10-08 10:20:09 -07001289 }
Darin Petkov73058b42010-10-06 16:32:19 -07001290 return true;
1291}
1292
Kelvin Zhangcfe694f2020-11-13 13:10:42 -05001293bool DeltaPerformer::ShouldCheckpoint() {
Sen Jiang53c2ec52019-01-10 15:27:59 -08001294 base::TimeTicks curr_time = base::TimeTicks::Now();
Kelvin Zhangcfe694f2020-11-13 13:10:42 -05001295 if (curr_time > update_checkpoint_time_) {
Colin Howes0e452c92018-11-02 13:18:44 -07001296 update_checkpoint_time_ = curr_time + update_checkpoint_wait_;
Kelvin Zhangcfe694f2020-11-13 13:10:42 -05001297 return true;
1298 }
1299 return false;
1300}
1301
1302bool DeltaPerformer::CheckpointUpdateProgress(bool force) {
1303 if (!force && !ShouldCheckpoint()) {
Colin Howes0e452c92018-11-02 13:18:44 -07001304 return false;
1305 }
Darin Petkov9c0baf82010-10-07 13:44:48 -07001306 Terminator::set_exit_blocked(true);
Kelvin Zhang3265b312020-12-16 15:51:30 -05001307 if (last_updated_operation_num_ != next_operation_num_ || force) {
Darin Petkov9c0baf82010-10-07 13:44:48 -07001308 // Resets the progress in case we die in the middle of the state update.
Darin Petkov9b230572010-10-08 10:20:09 -07001309 ResetUpdateProgress(prefs_, true);
Kelvin Zhangbb8e9992020-12-28 10:28:48 -05001310 if (!signatures_message_data_.empty()) {
1311 // Save the signature blob because if the update is interrupted after the
1312 // download phase we don't go through this path anymore. Some alternatives
1313 // to consider:
1314 //
1315 // 1. On resume, re-download the signature blob from the server and
1316 // re-verify it.
1317 //
1318 // 2. Verify the signature as soon as it's received and don't checkpoint
1319 // the blob and the signed sha-256 context.
1320 LOG_IF(WARNING,
1321 !prefs_->SetString(kPrefsUpdateStateSignatureBlob,
1322 signatures_message_data_))
1323 << "Unable to store the signature blob.";
1324 }
Amin Hassani008c4582019-01-13 16:22:47 -08001325 TEST_AND_RETURN_FALSE(prefs_->SetString(
1326 kPrefsUpdateStateSHA256Context, payload_hash_calculator_.GetContext()));
Sen Jiangf6813802015-11-03 21:27:29 -08001327 TEST_AND_RETURN_FALSE(
1328 prefs_->SetString(kPrefsUpdateStateSignedSHA256Context,
1329 signed_hash_calculator_.GetContext()));
Amin Hassani008c4582019-01-13 16:22:47 -08001330 TEST_AND_RETURN_FALSE(
1331 prefs_->SetInt64(kPrefsUpdateStateNextDataOffset, buffer_offset_));
Kelvin Zhangcfe694f2020-11-13 13:10:42 -05001332 last_updated_operation_num_ = next_operation_num_;
David Zeuthen41996ad2013-09-24 15:43:24 -07001333
1334 if (next_operation_num_ < num_total_operations_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -07001335 size_t partition_index = current_partition_;
Kelvin Zhangcfe694f2020-11-13 13:10:42 -05001336 while (next_operation_num_ >= acc_num_operations_[partition_index]) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -07001337 partition_index++;
Kelvin Zhangcfe694f2020-11-13 13:10:42 -05001338 }
Amin Hassani008c4582019-01-13 16:22:47 -08001339 const size_t partition_operation_num =
1340 next_operation_num_ -
1341 (partition_index ? acc_num_operations_[partition_index - 1] : 0);
Alex Deymoa12ee112015-08-12 22:19:32 -07001342 const InstallOperation& op =
Alex Deymoe5e5fe92015-10-05 09:28:19 -07001343 partitions_[partition_index].operations(partition_operation_num);
Amin Hassani008c4582019-01-13 16:22:47 -08001344 TEST_AND_RETURN_FALSE(
1345 prefs_->SetInt64(kPrefsUpdateStateNextDataLength, op.data_length()));
David Zeuthen41996ad2013-09-24 15:43:24 -07001346 } else {
Amin Hassani008c4582019-01-13 16:22:47 -08001347 TEST_AND_RETURN_FALSE(
1348 prefs_->SetInt64(kPrefsUpdateStateNextDataLength, 0));
David Zeuthen41996ad2013-09-24 15:43:24 -07001349 }
Kelvin Zhange4235b02020-11-23 13:57:51 -05001350 if (partition_writer_) {
1351 partition_writer_->CheckpointUpdateProgress(GetPartitionOperationNum());
1352 } else {
1353 CHECK_EQ(next_operation_num_, num_total_operations_)
1354 << "Partition writer is null, we are expected to finish all "
1355 "operations: "
1356 << next_operation_num_ << "/" << num_total_operations_;
1357 }
Darin Petkov0406e402010-10-06 21:33:11 -07001358 }
Amin Hassani008c4582019-01-13 16:22:47 -08001359 TEST_AND_RETURN_FALSE(
1360 prefs_->SetInt64(kPrefsUpdateStateNextOperation, next_operation_num_));
Darin Petkov73058b42010-10-06 16:32:19 -07001361 return true;
1362}
1363
Darin Petkov9b230572010-10-08 10:20:09 -07001364bool DeltaPerformer::PrimeUpdateState() {
1365 CHECK(manifest_valid_);
Darin Petkov9b230572010-10-08 10:20:09 -07001366
1367 int64_t next_operation = kUpdateStateOperationInvalid;
1368 if (!prefs_->GetInt64(kPrefsUpdateStateNextOperation, &next_operation) ||
Amin Hassani008c4582019-01-13 16:22:47 -08001369 next_operation == kUpdateStateOperationInvalid || next_operation <= 0) {
Darin Petkov9b230572010-10-08 10:20:09 -07001370 // Initiating a new update, no more state needs to be initialized.
1371 return true;
1372 }
1373 next_operation_num_ = next_operation;
1374
1375 // Resuming an update -- load the rest of the update state.
1376 int64_t next_data_offset = -1;
Amin Hassani008c4582019-01-13 16:22:47 -08001377 TEST_AND_RETURN_FALSE(
1378 prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset) &&
1379 next_data_offset >= 0);
Darin Petkov9b230572010-10-08 10:20:09 -07001380 buffer_offset_ = next_data_offset;
1381
Darin Petkov4f0a07b2011-05-25 16:47:20 -07001382 // The signed hash context and the signature blob may be empty if the
1383 // interrupted update didn't reach the signature.
Sen Jiangf6813802015-11-03 21:27:29 -08001384 string signed_hash_context;
1385 if (prefs_->GetString(kPrefsUpdateStateSignedSHA256Context,
1386 &signed_hash_context)) {
1387 TEST_AND_RETURN_FALSE(
1388 signed_hash_calculator_.SetContext(signed_hash_context));
1389 }
1390
Sen Jiang9b2f1782019-01-24 14:27:50 -08001391 prefs_->GetString(kPrefsUpdateStateSignatureBlob, &signatures_message_data_);
Darin Petkov9b230572010-10-08 10:20:09 -07001392
1393 string hash_context;
Amin Hassani008c4582019-01-13 16:22:47 -08001394 TEST_AND_RETURN_FALSE(
1395 prefs_->GetString(kPrefsUpdateStateSHA256Context, &hash_context) &&
1396 payload_hash_calculator_.SetContext(hash_context));
Darin Petkov9b230572010-10-08 10:20:09 -07001397
1398 int64_t manifest_metadata_size = 0;
Amin Hassani008c4582019-01-13 16:22:47 -08001399 TEST_AND_RETURN_FALSE(
1400 prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size) &&
1401 manifest_metadata_size > 0);
Gilad Arnoldfe133932014-01-14 12:25:50 -08001402 metadata_size_ = manifest_metadata_size;
Darin Petkov9b230572010-10-08 10:20:09 -07001403
Alex Deymof25eb492016-02-26 00:20:08 -08001404 int64_t manifest_signature_size = 0;
1405 TEST_AND_RETURN_FALSE(
1406 prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size) &&
1407 manifest_signature_size >= 0);
1408 metadata_signature_size_ = manifest_signature_size;
1409
Gilad Arnold8a86fa52013-01-15 12:35:05 -08001410 // Advance the download progress to reflect what doesn't need to be
1411 // re-downloaded.
1412 total_bytes_received_ += buffer_offset_;
1413
Darin Petkov61426142010-10-08 11:04:55 -07001414 // Speculatively count the resume as a failure.
1415 int64_t resumed_update_failures;
1416 if (prefs_->GetInt64(kPrefsResumedUpdateFailures, &resumed_update_failures)) {
1417 resumed_update_failures++;
1418 } else {
1419 resumed_update_failures = 1;
1420 }
1421 prefs_->SetInt64(kPrefsResumedUpdateFailures, resumed_update_failures);
Darin Petkov9b230572010-10-08 10:20:09 -07001422 return true;
1423}
1424
Kelvin Zhangebd115e2021-03-08 16:10:25 -05001425bool DeltaPerformer::IsDynamicPartition(const std::string& part_name,
1426 uint32_t slot) {
Tianjie3a55fc22021-02-13 16:02:22 -08001427 return boot_control_->GetDynamicPartitionControl()->IsDynamicPartition(
Kelvin Zhangebd115e2021-03-08 16:10:25 -05001428 part_name, slot);
Kelvin Zhang94f51cc2020-09-25 11:34:49 -04001429}
1430
Kelvin Zhange52b6cd2021-02-09 15:28:40 -05001431std::unique_ptr<PartitionWriterInterface> DeltaPerformer::CreatePartitionWriter(
Kelvin Zhangcfe694f2020-11-13 13:10:42 -05001432 const PartitionUpdate& partition_update,
1433 const InstallPlan::Partition& install_part,
1434 DynamicPartitionControlInterface* dynamic_control,
1435 size_t block_size,
1436 bool is_interactive,
1437 bool is_dynamic_partition) {
1438 return partition_writer::CreatePartitionWriter(
1439 partition_update,
1440 install_part,
1441 dynamic_control,
1442 block_size_,
1443 interactive_,
Kelvin Zhangebd115e2021-03-08 16:10:25 -05001444 IsDynamicPartition(install_part.name, install_plan_->target_slot));
Kelvin Zhangcfe694f2020-11-13 13:10:42 -05001445}
1446
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001447} // namespace chromeos_update_engine