| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2020 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
|  | 16 | #include <update_engine/payload_consumer/partition_writer.h> | 
|  | 17 |  | 
|  | 18 | #include <fcntl.h> | 
|  | 19 | #include <linux/fs.h> | 
| Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 20 | #include <sys/mman.h> | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 21 |  | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 22 | #include <inttypes.h> | 
|  | 23 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 24 | #include <algorithm> | 
|  | 25 | #include <initializer_list> | 
|  | 26 | #include <memory> | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 27 | #include <string> | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 28 | #include <utility> | 
|  | 29 | #include <vector> | 
|  | 30 |  | 
|  | 31 | #include <base/strings/string_number_conversions.h> | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 32 | #include <base/strings/string_util.h> | 
|  | 33 | #include <base/strings/stringprintf.h> | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 34 |  | 
|  | 35 | #include "update_engine/common/terminator.h" | 
|  | 36 | #include "update_engine/common/utils.h" | 
|  | 37 | #include "update_engine/payload_consumer/bzip_extent_writer.h" | 
|  | 38 | #include "update_engine/payload_consumer/cached_file_descriptor.h" | 
|  | 39 | #include "update_engine/payload_consumer/extent_reader.h" | 
|  | 40 | #include "update_engine/payload_consumer/extent_writer.h" | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 41 | #include "update_engine/payload_consumer/file_descriptor_utils.h" | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 42 | #include "update_engine/payload_consumer/install_operation_executor.h" | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 43 | #include "update_engine/payload_consumer/install_plan.h" | 
|  | 44 | #include "update_engine/payload_consumer/mount_history.h" | 
|  | 45 | #include "update_engine/payload_consumer/payload_constants.h" | 
|  | 46 | #include "update_engine/payload_consumer/xz_extent_writer.h" | 
| Kelvin Zhang | 37b9b70 | 2021-02-23 10:30:37 -0500 | [diff] [blame] | 47 | #include "update_engine/payload_generator/extent_utils.h" | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 48 |  | 
|  | 49 | namespace chromeos_update_engine { | 
|  | 50 |  | 
|  | 51 | namespace { | 
|  | 52 | constexpr uint64_t kCacheSize = 1024 * 1024;  // 1MB | 
|  | 53 |  | 
|  | 54 | // Discard the tail of the block device referenced by |fd|, from the offset | 
|  | 55 | // |data_size| until the end of the block device. Returns whether the data was | 
|  | 56 | // discarded. | 
|  | 57 |  | 
|  | 58 | bool DiscardPartitionTail(const FileDescriptorPtr& fd, uint64_t data_size) { | 
|  | 59 | uint64_t part_size = fd->BlockDevSize(); | 
|  | 60 | if (!part_size || part_size <= data_size) | 
|  | 61 | return false; | 
|  | 62 |  | 
|  | 63 | struct blkioctl_request { | 
|  | 64 | int number; | 
|  | 65 | const char* name; | 
|  | 66 | }; | 
|  | 67 | const std::initializer_list<blkioctl_request> blkioctl_requests = { | 
|  | 68 | {BLKDISCARD, "BLKDISCARD"}, | 
|  | 69 | {BLKSECDISCARD, "BLKSECDISCARD"}, | 
|  | 70 | #ifdef BLKZEROOUT | 
|  | 71 | {BLKZEROOUT, "BLKZEROOUT"}, | 
|  | 72 | #endif | 
|  | 73 | }; | 
|  | 74 | for (const auto& req : blkioctl_requests) { | 
|  | 75 | int error = 0; | 
|  | 76 | if (fd->BlkIoctl(req.number, data_size, part_size - data_size, &error) && | 
|  | 77 | error == 0) { | 
|  | 78 | return true; | 
|  | 79 | } | 
|  | 80 | LOG(WARNING) << "Error discarding the last " | 
|  | 81 | << (part_size - data_size) / 1024 << " KiB using ioctl(" | 
|  | 82 | << req.name << ")"; | 
|  | 83 | } | 
|  | 84 | return false; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | }  // namespace | 
|  | 88 |  | 
| Kelvin Zhang | 37b9b70 | 2021-02-23 10:30:37 -0500 | [diff] [blame] | 89 | using google::protobuf::RepeatedPtrField; | 
|  | 90 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 91 | // Opens path for read/write. On success returns an open FileDescriptor | 
|  | 92 | // and sets *err to 0. On failure, sets *err to errno and returns nullptr. | 
|  | 93 | FileDescriptorPtr OpenFile(const char* path, | 
|  | 94 | int mode, | 
|  | 95 | bool cache_writes, | 
|  | 96 | int* err) { | 
|  | 97 | // Try to mark the block device read-only based on the mode. Ignore any | 
|  | 98 | // failure since this won't work when passing regular files. | 
|  | 99 | bool read_only = (mode & O_ACCMODE) == O_RDONLY; | 
|  | 100 | utils::SetBlockDeviceReadOnly(path, read_only); | 
|  | 101 |  | 
|  | 102 | FileDescriptorPtr fd(new EintrSafeFileDescriptor()); | 
|  | 103 | if (cache_writes && !read_only) { | 
|  | 104 | fd = FileDescriptorPtr(new CachedFileDescriptor(fd, kCacheSize)); | 
|  | 105 | LOG(INFO) << "Caching writes."; | 
|  | 106 | } | 
|  | 107 | if (!fd->Open(path, mode, 000)) { | 
|  | 108 | *err = errno; | 
|  | 109 | PLOG(ERROR) << "Unable to open file " << path; | 
|  | 110 | return nullptr; | 
|  | 111 | } | 
|  | 112 | *err = 0; | 
|  | 113 | return fd; | 
|  | 114 | } | 
|  | 115 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 116 | PartitionWriter::PartitionWriter( | 
|  | 117 | const PartitionUpdate& partition_update, | 
|  | 118 | const InstallPlan::Partition& install_part, | 
|  | 119 | DynamicPartitionControlInterface* dynamic_control, | 
|  | 120 | size_t block_size, | 
|  | 121 | bool is_interactive) | 
|  | 122 | : partition_update_(partition_update), | 
|  | 123 | install_part_(install_part), | 
|  | 124 | dynamic_control_(dynamic_control), | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 125 | verified_source_fd_(block_size, install_part.source_path), | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 126 | interactive_(is_interactive), | 
| Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 127 | block_size_(block_size), | 
|  | 128 | install_op_executor_(block_size) {} | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 129 |  | 
|  | 130 | PartitionWriter::~PartitionWriter() { | 
|  | 131 | Close(); | 
|  | 132 | } | 
|  | 133 |  | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 134 | bool PartitionWriter::OpenSourcePartition(uint32_t source_slot, | 
|  | 135 | bool source_may_exist) { | 
|  | 136 | source_path_.clear(); | 
|  | 137 | if (!source_may_exist) { | 
|  | 138 | return true; | 
|  | 139 | } | 
|  | 140 | if (install_part_.source_size > 0 && !install_part_.source_path.empty()) { | 
|  | 141 | source_path_ = install_part_.source_path; | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 142 | if (!verified_source_fd_.Open()) { | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 143 | LOG(ERROR) << "Unable to open source partition " << install_part_.name | 
|  | 144 | << " on slot " << BootControlInterface::SlotName(source_slot) | 
|  | 145 | << ", file " << source_path_; | 
|  | 146 | return false; | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 | return true; | 
|  | 150 | } | 
|  | 151 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 152 | bool PartitionWriter::Init(const InstallPlan* install_plan, | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 153 | bool source_may_exist, | 
|  | 154 | size_t next_op_index) { | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 155 | const PartitionUpdate& partition = partition_update_; | 
|  | 156 | uint32_t source_slot = install_plan->source_slot; | 
|  | 157 | uint32_t target_slot = install_plan->target_slot; | 
| Kelvin Zhang | 3f60d53 | 2020-11-09 13:33:17 -0500 | [diff] [blame] | 158 | TEST_AND_RETURN_FALSE(OpenSourcePartition(source_slot, source_may_exist)); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 159 |  | 
|  | 160 | // We shouldn't open the source partition in certain cases, e.g. some dynamic | 
|  | 161 | // partitions in delta payload, partitions included in the full payload for | 
|  | 162 | // partial updates. Use the source size as the indicator. | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 163 |  | 
|  | 164 | target_path_ = install_part_.target_path; | 
|  | 165 | int err; | 
|  | 166 |  | 
|  | 167 | int flags = O_RDWR; | 
|  | 168 | if (!interactive_) | 
|  | 169 | flags |= O_DSYNC; | 
|  | 170 |  | 
|  | 171 | LOG(INFO) << "Opening " << target_path_ << " partition with" | 
|  | 172 | << (interactive_ ? "out" : "") << " O_DSYNC"; | 
|  | 173 |  | 
|  | 174 | target_fd_ = OpenFile(target_path_.c_str(), flags, true, &err); | 
|  | 175 | if (!target_fd_) { | 
|  | 176 | LOG(ERROR) << "Unable to open target partition " | 
|  | 177 | << partition.partition_name() << " on slot " | 
|  | 178 | << BootControlInterface::SlotName(target_slot) << ", file " | 
|  | 179 | << target_path_; | 
|  | 180 | return false; | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | LOG(INFO) << "Applying " << partition.operations().size() | 
|  | 184 | << " operations to partition \"" << partition.partition_name() | 
|  | 185 | << "\""; | 
|  | 186 |  | 
|  | 187 | // Discard the end of the partition, but ignore failures. | 
|  | 188 | DiscardPartitionTail(target_fd_, install_part_.target_size); | 
|  | 189 |  | 
|  | 190 | return true; | 
|  | 191 | } | 
|  | 192 |  | 
| Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 193 | bool PartitionWriter::PerformReplaceOperation(const InstallOperation& operation, | 
|  | 194 | const void* data, | 
|  | 195 | size_t count) { | 
|  | 196 | // Setup the ExtentWriter stack based on the operation type. | 
|  | 197 | std::unique_ptr<ExtentWriter> writer = CreateBaseExtentWriter(); | 
|  | 198 | writer->Init(operation.dst_extents(), block_size_); | 
|  | 199 | return install_op_executor_.ExecuteReplaceOperation( | 
|  | 200 | operation, std::move(writer), data, count); | 
|  | 201 | } | 
|  | 202 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 203 | bool PartitionWriter::PerformZeroOrDiscardOperation( | 
|  | 204 | const InstallOperation& operation) { | 
|  | 205 | #ifdef BLKZEROOUT | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 206 | int request = | 
|  | 207 | (operation.type() == InstallOperation::ZERO ? BLKZEROOUT : BLKDISCARD); | 
|  | 208 | #else   // !defined(BLKZEROOUT) | 
| Kelvin Zhang | f457586 | 2021-02-24 10:46:32 -0500 | [diff] [blame] | 209 | auto writer = CreateBaseExtentWriter(); | 
|  | 210 | return install_op_executor_.ExecuteZeroOrDiscardOperation(operation, | 
|  | 211 | writer.get()); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 212 | #endif  // !defined(BLKZEROOUT) | 
|  | 213 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 214 | for (const Extent& extent : operation.dst_extents()) { | 
|  | 215 | const uint64_t start = extent.start_block() * block_size_; | 
|  | 216 | const uint64_t length = extent.num_blocks() * block_size_; | 
| Kelvin Zhang | f457586 | 2021-02-24 10:46:32 -0500 | [diff] [blame] | 217 | int result = 0; | 
|  | 218 | if (target_fd_->BlkIoctl(request, start, length, &result) && result == 0) { | 
|  | 219 | continue; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 220 | } | 
| Kelvin Zhang | f457586 | 2021-02-24 10:46:32 -0500 | [diff] [blame] | 221 | // In case of failure, we fall back to writing 0 for the entire operation. | 
|  | 222 | PLOG(WARNING) << "BlkIoctl failed. Falling back to write 0s for remainder " | 
|  | 223 | "of this operation."; | 
|  | 224 | auto writer = CreateBaseExtentWriter(); | 
|  | 225 | writer->Init(operation.dst_extents(), block_size_); | 
| Kelvin Zhang | 23dfcd2 | 2021-08-31 16:32:49 -0700 | [diff] [blame] | 226 | return install_op_executor_.ExecuteZeroOrDiscardOperation( | 
|  | 227 | operation, std::move(writer)); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 228 | } | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 229 | return true; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 230 | } | 
|  | 231 |  | 
|  | 232 | bool PartitionWriter::PerformSourceCopyOperation( | 
|  | 233 | const InstallOperation& operation, ErrorCode* error) { | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 234 | // The device may optimize the SOURCE_COPY operation. | 
|  | 235 | // Being this a device-specific optimization let DynamicPartitionController | 
|  | 236 | // decide it the operation should be skipped. | 
|  | 237 | const PartitionUpdate& partition = partition_update_; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 238 |  | 
|  | 239 | InstallOperation buf; | 
| Kelvin Zhang | 37b9b70 | 2021-02-23 10:30:37 -0500 | [diff] [blame] | 240 | const bool should_optimize = dynamic_control_->OptimizeOperation( | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 241 | partition.partition_name(), operation, &buf); | 
|  | 242 | const InstallOperation& optimized = should_optimize ? buf : operation; | 
|  | 243 |  | 
| Kelvin Zhang | 37b9b70 | 2021-02-23 10:30:37 -0500 | [diff] [blame] | 244 | // Invoke ChooseSourceFD with original operation, so that it can properly | 
|  | 245 | // verify source hashes. Optimized operation might contain a smaller set of | 
|  | 246 | // extents, or completely empty. | 
|  | 247 | auto source_fd = ChooseSourceFD(operation, error); | 
|  | 248 | if (source_fd == nullptr) { | 
|  | 249 | LOG(ERROR) << "Unrecoverable source hash mismatch found on partition " | 
|  | 250 | << partition.partition_name() | 
| Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 251 | << " extents: " << ExtentsToString(operation.src_extents()); | 
| Kelvin Zhang | 37b9b70 | 2021-02-23 10:30:37 -0500 | [diff] [blame] | 252 | return false; | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 253 | } | 
| Kelvin Zhang | 37b9b70 | 2021-02-23 10:30:37 -0500 | [diff] [blame] | 254 |  | 
| Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 255 | auto writer = CreateBaseExtentWriter(); | 
|  | 256 | writer->Init(optimized.dst_extents(), block_size_); | 
|  | 257 | return install_op_executor_.ExecuteSourceCopyOperation( | 
| Kelvin Zhang | 23dfcd2 | 2021-08-31 16:32:49 -0700 | [diff] [blame] | 258 | optimized, std::move(writer), source_fd); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 259 | } | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 260 |  | 
| Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 261 | bool PartitionWriter::PerformDiffOperation(const InstallOperation& operation, | 
|  | 262 | ErrorCode* error, | 
|  | 263 | const void* data, | 
|  | 264 | size_t count) { | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 265 | FileDescriptorPtr source_fd = ChooseSourceFD(operation, error); | 
|  | 266 | TEST_AND_RETURN_FALSE(source_fd != nullptr); | 
|  | 267 |  | 
| Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 268 | auto writer = CreateBaseExtentWriter(); | 
|  | 269 | writer->Init(operation.dst_extents(), block_size_); | 
| Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 270 | return install_op_executor_.ExecuteDiffOperation( | 
| Kelvin Zhang | 1d402cb | 2021-02-09 15:28:16 -0500 | [diff] [blame] | 271 | operation, std::move(writer), source_fd, data, count); | 
|  | 272 | } | 
|  | 273 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 274 | FileDescriptorPtr PartitionWriter::ChooseSourceFD( | 
|  | 275 | const InstallOperation& operation, ErrorCode* error) { | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 276 | return verified_source_fd_.ChooseSourceFD(operation, error); | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
|  | 279 | int PartitionWriter::Close() { | 
|  | 280 | int err = 0; | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 281 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 282 | source_path_.clear(); | 
|  | 283 |  | 
|  | 284 | if (target_fd_ && !target_fd_->Close()) { | 
|  | 285 | err = errno; | 
|  | 286 | PLOG(ERROR) << "Error closing target partition"; | 
|  | 287 | if (!err) | 
|  | 288 | err = 1; | 
|  | 289 | } | 
|  | 290 | target_fd_.reset(); | 
|  | 291 | target_path_.clear(); | 
|  | 292 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 293 | return -err; | 
|  | 294 | } | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 295 |  | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 296 | void PartitionWriter::CheckpointUpdateProgress(size_t next_op_index) { | 
|  | 297 | target_fd_->Flush(); | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
| Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 300 | std::unique_ptr<ExtentWriter> PartitionWriter::CreateBaseExtentWriter() { | 
| Kelvin Zhang | 4d22ca2 | 2021-02-09 14:06:25 -0500 | [diff] [blame] | 301 | return std::make_unique<DirectExtentWriter>(target_fd_); | 
| Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 302 | } | 
|  | 303 |  | 
| Kelvin Zhang | ab3ce60 | 2021-02-24 14:46:40 -0500 | [diff] [blame] | 304 | bool PartitionWriter::ValidateSourceHash(const brillo::Blob& calculated_hash, | 
|  | 305 | const InstallOperation& operation, | 
|  | 306 | const FileDescriptorPtr source_fd, | 
|  | 307 | ErrorCode* error) { | 
|  | 308 | using std::string; | 
|  | 309 | using std::vector; | 
|  | 310 | brillo::Blob expected_source_hash(operation.src_sha256_hash().begin(), | 
|  | 311 | operation.src_sha256_hash().end()); | 
|  | 312 | if (calculated_hash != expected_source_hash) { | 
|  | 313 | LOG(ERROR) << "The hash of the source data on disk for this operation " | 
|  | 314 | << "doesn't match the expected value. This could mean that the " | 
|  | 315 | << "delta update payload was targeted for another version, or " | 
|  | 316 | << "that the source partition was modified after it was " | 
|  | 317 | << "installed, for example, by mounting a filesystem."; | 
|  | 318 | LOG(ERROR) << "Expected:   sha256|hex = " | 
|  | 319 | << base::HexEncode(expected_source_hash.data(), | 
|  | 320 | expected_source_hash.size()); | 
|  | 321 | LOG(ERROR) << "Calculated: sha256|hex = " | 
|  | 322 | << base::HexEncode(calculated_hash.data(), | 
|  | 323 | calculated_hash.size()); | 
|  | 324 |  | 
|  | 325 | vector<string> source_extents; | 
|  | 326 | for (const Extent& ext : operation.src_extents()) { | 
|  | 327 | source_extents.push_back( | 
|  | 328 | base::StringPrintf("%" PRIu64 ":%" PRIu64, | 
|  | 329 | static_cast<uint64_t>(ext.start_block()), | 
|  | 330 | static_cast<uint64_t>(ext.num_blocks()))); | 
|  | 331 | } | 
|  | 332 | LOG(ERROR) << "Operation source (offset:size) in blocks: " | 
|  | 333 | << base::JoinString(source_extents, ","); | 
|  | 334 |  | 
|  | 335 | // Log remount history if this device is an ext4 partition. | 
|  | 336 | LogMountHistory(source_fd); | 
|  | 337 |  | 
|  | 338 | *error = ErrorCode::kDownloadStateInitializationError; | 
|  | 339 | return false; | 
|  | 340 | } | 
|  | 341 | return true; | 
|  | 342 | } | 
|  | 343 |  | 
| Kelvin Zhang | 50bac65 | 2020-09-28 15:51:41 -0400 | [diff] [blame] | 344 | }  // namespace chromeos_update_engine |