blob: 09dc638b9c8a2c98c55cfa39a644e0c0489d71ad [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//
Allie Woodeb9e6d82015-04-17 13:55:30 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/filesystem_verifier_action.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070018
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040023#include <unistd.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070024
25#include <algorithm>
26#include <cstdlib>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040027#include <memory>
Allie Woodeb9e6d82015-04-17 13:55:30 -070028#include <string>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040029#include <utility>
Allie Woodeb9e6d82015-04-17 13:55:30 -070030
Alex Deymo20c99202015-07-09 16:14:16 -070031#include <base/bind.h>
Tianjie24f96092020-06-30 12:26:25 -070032#include <base/strings/string_util.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040033#include <brillo/data_encoding.h>
34#include <brillo/message_loops/message_loop.h>
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -050035#include <brillo/secure_blob.h>
Kelvin Zhangec205cf2020-09-28 13:23:40 -040036#include <brillo/streams/file_stream.h>
Allie Woodeb9e6d82015-04-17 13:55:30 -070037
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -050038#include "payload_generator/delta_diff_generator.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080039#include "update_engine/common/utils.h"
Kelvin Zhangec205cf2020-09-28 13:23:40 -040040#include "update_engine/payload_consumer/file_descriptor.h"
Allie Woodeb9e6d82015-04-17 13:55:30 -070041
Sen Jiang2703ef42017-03-16 13:36:21 -070042using brillo::data_encoding::Base64Encode;
Allie Woodeb9e6d82015-04-17 13:55:30 -070043using std::string;
44
Kelvin Zhang7f925672021-03-15 13:37:40 -040045// On a partition with verity enabled, we expect to see the following format:
46// ===================================================
47// Normal Filesystem Data
48// (this should take most of the space, like over 90%)
49// ===================================================
50// Hash tree
51// ~0.8% (e.g. 16M for 2GB image)
52// ===================================================
53// FEC data
54// ~0.8%
55// ===================================================
56// Footer
57// 4K
58// ===================================================
59
60// For OTA that doesn't do on device verity computation, hash tree and fec data
61// are written during DownloadAction as a regular InstallOp, so no special
62// handling needed, we can just read the entire partition in 1 go.
63
64// Verity enabled case: Only Normal FS data is written during download action.
65// When hasing the entire partition, we will need to build the hash tree, write
66// it to disk, then build FEC, and write it to disk. Therefore, it is important
67// that we finish writing hash tree before we attempt to read & hash it. The
68// same principal applies to FEC data.
69
70// |verity_writer_| handles building and
71// writing of FEC/HashTree, we just need to be careful when reading.
72// Specifically, we must stop at beginning of Hash tree, let |verity_writer_|
73// write both hash tree and FEC, then continue reading the remaining part of
74// partition.
75
Allie Woodeb9e6d82015-04-17 13:55:30 -070076namespace chromeos_update_engine {
77
78namespace {
Alex Deymo20c99202015-07-09 16:14:16 -070079const off_t kReadFileBufferSize = 128 * 1024;
Allie Woodeb9e6d82015-04-17 13:55:30 -070080} // namespace
81
Allie Woodeb9e6d82015-04-17 13:55:30 -070082void FilesystemVerifierAction::PerformAction() {
83 // Will tell the ActionProcessor we've failed if we return.
84 ScopedActionCompleter abort_action_completer(processor_, this);
85
86 if (!HasInputObject()) {
87 LOG(ERROR) << "FilesystemVerifierAction missing input object.";
88 return;
89 }
90 install_plan_ = GetInputObject();
91
Alex Deymoe5e5fe92015-10-05 09:28:19 -070092 if (install_plan_.partitions.empty()) {
93 LOG(INFO) << "No partitions to verify.";
Allie Woodeb9e6d82015-04-17 13:55:30 -070094 if (HasOutputPipe())
95 SetOutputObject(install_plan_);
96 abort_action_completer.set_code(ErrorCode::kSuccess);
97 return;
98 }
Jae Hoon Kim50504d62020-04-23 14:32:38 -070099 install_plan_.Dump();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700100 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700101 abort_action_completer.set_should_complete(false);
102}
103
104void FilesystemVerifierAction::TerminateProcessing() {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400105 brillo::MessageLoop::current()->CancelTask(pending_task_id_);
Alex Deymo20c99202015-07-09 16:14:16 -0700106 cancelled_ = true;
107 Cleanup(ErrorCode::kSuccess); // error code is ignored if canceled_ is true.
Allie Woodeb9e6d82015-04-17 13:55:30 -0700108}
109
Allie Woodeb9e6d82015-04-17 13:55:30 -0700110void FilesystemVerifierAction::Cleanup(ErrorCode code) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500111 partition_fd_.reset();
Alex Deymo20c99202015-07-09 16:14:16 -0700112 // This memory is not used anymore.
113 buffer_.clear();
114
Allie Woodeb9e6d82015-04-17 13:55:30 -0700115 if (cancelled_)
116 return;
117 if (code == ErrorCode::kSuccess && HasOutputPipe())
118 SetOutputObject(install_plan_);
Kelvin Zhang70eef232020-06-12 20:32:40 +0000119 UpdateProgress(1.0);
Allie Woodeb9e6d82015-04-17 13:55:30 -0700120 processor_->ActionComplete(this, code);
121}
122
Kelvin Zhang70eef232020-06-12 20:32:40 +0000123void FilesystemVerifierAction::UpdateProgress(double progress) {
124 if (delegate_ != nullptr) {
125 delegate_->OnVerifyProgressUpdate(progress);
126 }
127}
128
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400129bool FilesystemVerifierAction::InitializeFdVABC() {
130 const InstallPlan::Partition& partition =
131 install_plan_.partitions[partition_index_];
132
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500133 // FilesystemVerifierAction need the read_fd_.
134 partition_fd_ =
Kelvin Zhang21a49912021-03-12 14:28:33 -0500135 dynamic_control_->OpenCowFd(partition.name, partition.source_path, true);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500136 if (!partition_fd_) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400137 LOG(ERROR) << "OpenCowReader(" << partition.name << ", "
138 << partition.source_path << ") failed.";
139 return false;
140 }
141 partition_size_ = partition.target_size;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400142 return true;
143}
144
145bool FilesystemVerifierAction::InitializeFd(const std::string& part_path) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500146 partition_fd_ = FileDescriptorPtr(new EintrSafeFileDescriptor());
147 const bool write_verity = ShouldWriteVerity();
148 int flags = write_verity ? O_RDWR : O_RDONLY;
149 if (!utils::SetBlockDeviceReadOnly(part_path, !write_verity)) {
150 LOG(WARNING) << "Failed to set block device " << part_path << " as "
151 << (write_verity ? "writable" : "readonly");
152 }
153 if (!partition_fd_->Open(part_path.c_str(), flags)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400154 LOG(ERROR) << "Unable to open " << part_path << " for reading.";
155 return false;
156 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400157 return true;
158}
159
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700160void FilesystemVerifierAction::StartPartitionHashing() {
161 if (partition_index_ == install_plan_.partitions.size()) {
Tianjie24f96092020-06-30 12:26:25 -0700162 if (!install_plan_.untouched_dynamic_partitions.empty()) {
163 LOG(INFO) << "Verifying extents of untouched dynamic partitions ["
164 << base::JoinString(install_plan_.untouched_dynamic_partitions,
165 ", ")
166 << "]";
167 if (!dynamic_control_->VerifyExtentsForUntouchedPartitions(
168 install_plan_.source_slot,
169 install_plan_.target_slot,
170 install_plan_.untouched_dynamic_partitions)) {
171 Cleanup(ErrorCode::kFilesystemVerifierError);
172 return;
173 }
174 }
175
Sen Jianga35896c2016-05-25 11:08:41 -0700176 Cleanup(ErrorCode::kSuccess);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700177 return;
178 }
Sen Jiang57f91802017-11-14 17:42:13 -0800179 const InstallPlan::Partition& partition =
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700180 install_plan_.partitions[partition_index_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700181 string part_path;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700182 switch (verifier_step_) {
183 case VerifierStep::kVerifySourceHash:
Sen Jiange6e4bb92016-04-05 14:59:12 -0700184 part_path = partition.source_path;
Sen Jiang57f91802017-11-14 17:42:13 -0800185 partition_size_ = partition.source_size;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700186 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700187 case VerifierStep::kVerifyTargetHash:
Sen Jiange6e4bb92016-04-05 14:59:12 -0700188 part_path = partition.target_path;
Sen Jiang57f91802017-11-14 17:42:13 -0800189 partition_size_ = partition.target_size;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700190 break;
191 }
Yifan Hong537802d2018-08-15 13:15:42 -0700192
Yifan Hong537802d2018-08-15 13:15:42 -0700193 LOG(INFO) << "Hashing partition " << partition_index_ << " ("
194 << partition.name << ") on device " << part_path;
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400195 auto success = false;
Kelvin Zhang06188352021-02-10 13:21:47 -0500196 if (dynamic_control_->UpdateUsesSnapshotCompression() &&
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500197 verifier_step_ == VerifierStep::kVerifyTargetHash &&
198 dynamic_control_->IsDynamicPartition(partition.name,
199 install_plan_.target_slot)) {
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400200 success = InitializeFdVABC();
201 } else {
202 if (part_path.empty()) {
203 if (partition_size_ == 0) {
204 LOG(INFO) << "Skip hashing partition " << partition_index_ << " ("
205 << partition.name << ") because size is 0.";
206 partition_index_++;
207 StartPartitionHashing();
208 return;
209 }
210 LOG(ERROR) << "Cannot hash partition " << partition_index_ << " ("
211 << partition.name
212 << ") because its device path cannot be determined.";
213 Cleanup(ErrorCode::kFilesystemVerifierError);
214 return;
215 }
216 success = InitializeFd(part_path);
217 }
218 if (!success) {
Sen Jiang57f91802017-11-14 17:42:13 -0800219 Cleanup(ErrorCode::kFilesystemVerifierError);
220 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700221 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700222 buffer_.resize(kReadFileBufferSize);
Sen Jiang57f91802017-11-14 17:42:13 -0800223 hasher_ = std::make_unique<HashCalculator>();
224
225 offset_ = 0;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400226 filesystem_data_end_ = partition_size_;
227 CHECK_LE(partition.hash_tree_offset, partition.fec_offset)
228 << " Hash tree is expected to come before FEC data";
229 if (partition.hash_tree_offset != 0) {
230 filesystem_data_end_ = partition.hash_tree_offset;
231 } else if (partition.fec_offset != 0) {
232 filesystem_data_end_ = partition.fec_offset;
233 }
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400234 if (ShouldWriteVerity()) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400235 if (!verity_writer_->Init(partition)) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500236 LOG(INFO) << "Verity writes enabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800237 Cleanup(ErrorCode::kVerityCalculationError);
238 return;
239 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500240 } else {
241 LOG(INFO) << "Verity writes disabled on partition " << partition.name;
Sen Jiang57f91802017-11-14 17:42:13 -0800242 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700243
244 // Start the first read.
Kelvin Zhang7f925672021-03-15 13:37:40 -0400245 ScheduleFileSystemRead();
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700246}
247
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400248bool FilesystemVerifierAction::ShouldWriteVerity() {
249 const InstallPlan::Partition& partition =
250 install_plan_.partitions[partition_index_];
251 return verifier_step_ == VerifierStep::kVerifyTargetHash &&
252 install_plan_.write_verity &&
253 (partition.hash_tree_size > 0 || partition.fec_size > 0);
254}
255
Kelvin Zhang7f925672021-03-15 13:37:40 -0400256void FilesystemVerifierAction::ReadVerityAndFooter() {
257 if (ShouldWriteVerity()) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500258 if (!verity_writer_->Finalize(partition_fd_, partition_fd_)) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400259 LOG(ERROR) << "Failed to write hashtree/FEC data.";
260 Cleanup(ErrorCode::kFilesystemVerifierError);
261 return;
262 }
263 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500264 // Since we handed our |read_fd_| to verity_writer_ during |Finalize()|
265 // call, fd's position could have been changed. Re-seek.
266 partition_fd_->Seek(filesystem_data_end_, SEEK_SET);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400267 auto bytes_to_read = partition_size_ - filesystem_data_end_;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400268 while (bytes_to_read > 0) {
269 const auto read_size = std::min<size_t>(buffer_.size(), bytes_to_read);
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500270 auto bytes_read = partition_fd_->Read(buffer_.data(), read_size);
Kelvin Zhang7f925672021-03-15 13:37:40 -0400271 if (bytes_read <= 0) {
272 PLOG(ERROR) << "Failed to read hash tree " << bytes_read;
273 Cleanup(ErrorCode::kFilesystemVerifierError);
274 return;
275 }
276 if (!hasher_->Update(buffer_.data(), bytes_read)) {
277 LOG(ERROR) << "Unable to update the hash.";
278 Cleanup(ErrorCode::kError);
279 return;
280 }
281 bytes_to_read -= bytes_read;
282 }
283 FinishPartitionHashing();
284}
Sen Jiang57f91802017-11-14 17:42:13 -0800285
Kelvin Zhang7f925672021-03-15 13:37:40 -0400286void FilesystemVerifierAction::ScheduleFileSystemRead() {
Sen Jiang57f91802017-11-14 17:42:13 -0800287 // We can only start reading anything past |hash_tree_offset| after we have
288 // already read all the data blocks that the hash tree covers. The same
289 // applies to FEC.
Kelvin Zhang7f925672021-03-15 13:37:40 -0400290
291 size_t bytes_to_read = std::min(static_cast<uint64_t>(buffer_.size()),
292 filesystem_data_end_ - offset_);
Alex Deymob9e8e262015-08-03 20:23:03 -0700293 if (!bytes_to_read) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400294 ReadVerityAndFooter();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700295 return;
296 }
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500297 partition_fd_->Seek(offset_, SEEK_SET);
298 auto bytes_read = partition_fd_->Read(buffer_.data(), bytes_to_read);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400299 if (bytes_read < 0) {
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500300 LOG(ERROR) << "Unable to schedule an asynchronous read from the stream. "
301 << bytes_read;
Alex Deymob9e8e262015-08-03 20:23:03 -0700302 Cleanup(ErrorCode::kError);
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400303 } else {
304 // We could just invoke |OnReadDoneCallback()|, it works. But |PostTask|
305 // is used so that users can cancel updates.
306 pending_task_id_ = brillo::MessageLoop::current()->PostTask(
307 base::Bind(&FilesystemVerifierAction::OnReadDone,
308 base::Unretained(this),
309 bytes_read));
Allie Woodeb9e6d82015-04-17 13:55:30 -0700310 }
311}
312
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400313void FilesystemVerifierAction::OnReadDone(size_t bytes_read) {
Sen Jiang57f91802017-11-14 17:42:13 -0800314 if (cancelled_) {
315 Cleanup(ErrorCode::kError);
316 return;
317 }
Alex Deymob9e8e262015-08-03 20:23:03 -0700318 if (bytes_read == 0) {
Sen Jiang57f91802017-11-14 17:42:13 -0800319 LOG(ERROR) << "Failed to read the remaining " << partition_size_ - offset_
320 << " bytes from partition "
321 << install_plan_.partitions[partition_index_].name;
322 Cleanup(ErrorCode::kFilesystemVerifierError);
323 return;
324 }
325
326 if (!hasher_->Update(buffer_.data(), bytes_read)) {
327 LOG(ERROR) << "Unable to update the hash.";
328 Cleanup(ErrorCode::kError);
329 return;
330 }
331
Kelvin Zhang70eef232020-06-12 20:32:40 +0000332 // WE don't consider sizes of each partition. Every partition
333 // has the same length on progress bar.
334 // TODO(zhangkelvin) Take sizes of each partition into account
335
336 UpdateProgress(
337 (static_cast<double>(offset_) / partition_size_ + partition_index_) /
338 install_plan_.partitions.size());
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400339 if (ShouldWriteVerity()) {
Sen Jiang57f91802017-11-14 17:42:13 -0800340 if (!verity_writer_->Update(offset_, buffer_.data(), bytes_read)) {
Kelvin Zhang7f925672021-03-15 13:37:40 -0400341 LOG(ERROR) << "Unable to update verity";
Sen Jiang57f91802017-11-14 17:42:13 -0800342 Cleanup(ErrorCode::kVerityCalculationError);
Alex Deymob9e8e262015-08-03 20:23:03 -0700343 return;
344 }
345 }
346
Sen Jiang57f91802017-11-14 17:42:13 -0800347 offset_ += bytes_read;
Kelvin Zhang7f925672021-03-15 13:37:40 -0400348 if (offset_ == filesystem_data_end_) {
349 ReadVerityAndFooter();
Sen Jiang57f91802017-11-14 17:42:13 -0800350 return;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700351 }
Sen Jiang57f91802017-11-14 17:42:13 -0800352
Kelvin Zhang7f925672021-03-15 13:37:40 -0400353 ScheduleFileSystemRead();
Alex Deymob9e8e262015-08-03 20:23:03 -0700354}
355
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700356void FilesystemVerifierAction::FinishPartitionHashing() {
357 if (!hasher_->Finalize()) {
Alex Deymob9e8e262015-08-03 20:23:03 -0700358 LOG(ERROR) << "Unable to finalize the hash.";
Sen Jiang57f91802017-11-14 17:42:13 -0800359 Cleanup(ErrorCode::kError);
360 return;
Alex Deymob9e8e262015-08-03 20:23:03 -0700361 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700362 InstallPlan::Partition& partition =
363 install_plan_.partitions[partition_index_];
Sen Jiang2703ef42017-03-16 13:36:21 -0700364 LOG(INFO) << "Hash of " << partition.name << ": "
365 << Base64Encode(hasher_->raw_hash());
Alex Deymob9e8e262015-08-03 20:23:03 -0700366
Sen Jiangfef85fd2016-03-25 15:32:49 -0700367 switch (verifier_step_) {
368 case VerifierStep::kVerifyTargetHash:
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700369 if (partition.target_hash != hasher_->raw_hash()) {
370 LOG(ERROR) << "New '" << partition.name
371 << "' partition verification failed.";
Sen Jiangcdd52062017-05-18 15:33:10 -0700372 if (partition.source_hash.empty()) {
373 // No need to verify source if it is a full payload.
Sen Jiang57f91802017-11-14 17:42:13 -0800374 Cleanup(ErrorCode::kNewRootfsVerificationError);
375 return;
Sen Jiangcdd52062017-05-18 15:33:10 -0700376 }
Sen Jiangfef85fd2016-03-25 15:32:49 -0700377 // If we have not verified source partition yet, now that the target
Sen Jiang65566a32016-04-06 13:35:36 -0700378 // partition does not match, and it's not a full payload, we need to
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400379 // switch to kVerifySourceHash step to check if it's because the
380 // source partition does not match either.
Sen Jiangfef85fd2016-03-25 15:32:49 -0700381 verifier_step_ = VerifierStep::kVerifySourceHash;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800382 } else {
383 partition_index_++;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700384 }
385 break;
Sen Jiangfef85fd2016-03-25 15:32:49 -0700386 case VerifierStep::kVerifySourceHash:
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800387 if (partition.source_hash != hasher_->raw_hash()) {
388 LOG(ERROR) << "Old '" << partition.name
389 << "' partition verification failed.";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700390 LOG(ERROR) << "This is a server-side error due to mismatched delta"
391 << " update image!";
392 LOG(ERROR) << "The delta I've been given contains a " << partition.name
393 << " delta update that must be applied over a "
394 << partition.name << " with a specific checksum, but the "
395 << partition.name
396 << " we're starting with doesn't have that checksum! This"
397 " means that the delta I've been given doesn't match my"
398 " existing system. The "
399 << partition.name << " partition I have has hash: "
Sen Jiang2703ef42017-03-16 13:36:21 -0700400 << Base64Encode(hasher_->raw_hash())
Sen Jiangfef85fd2016-03-25 15:32:49 -0700401 << " but the update expected me to have "
Sen Jiang2703ef42017-03-16 13:36:21 -0700402 << Base64Encode(partition.source_hash) << " .";
Sen Jiangfef85fd2016-03-25 15:32:49 -0700403 LOG(INFO) << "To get the checksum of the " << partition.name
404 << " partition run this command: dd if="
405 << partition.source_path
406 << " bs=1M count=" << partition.source_size
407 << " iflag=count_bytes 2>/dev/null | openssl dgst -sha256 "
408 "-binary | openssl base64";
409 LOG(INFO) << "To get the checksum of partitions in a bin file, "
410 << "run: .../src/scripts/sha256_partitions.sh .../file.bin";
Sen Jiang57f91802017-11-14 17:42:13 -0800411 Cleanup(ErrorCode::kDownloadStateInitializationError);
412 return;
Sen Jiang1ad42ad2015-11-17 15:04:02 -0800413 }
Sen Jianga35896c2016-05-25 11:08:41 -0700414 // The action will skip kVerifySourceHash step if target partition hash
415 // matches, if we are in this step, it means target hash does not match,
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400416 // and now that the source partition hash matches, we should set the
417 // error code to reflect the error in target partition. We only need to
418 // verify the source partition which the target hash does not match, the
419 // rest of the partitions don't matter.
Sen Jiang57f91802017-11-14 17:42:13 -0800420 Cleanup(ErrorCode::kNewRootfsVerificationError);
421 return;
Allie Woodeb9e6d82015-04-17 13:55:30 -0700422 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700423 // Start hashing the next partition, if any.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700424 hasher_.reset();
425 buffer_.clear();
Kelvin Zhang4f28a6c2021-01-14 14:04:57 -0500426 if (partition_fd_) {
427 partition_fd_.reset();
Kelvin Zhangec205cf2020-09-28 13:23:40 -0400428 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700429 StartPartitionHashing();
Allie Woodeb9e6d82015-04-17 13:55:30 -0700430}
431
432} // namespace chromeos_update_engine