Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/delta_performer.h" |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 6 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 7 | #include <endian.h> |
| 8 | #include <errno.h> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 9 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <cstring> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 15 | #include <base/scoped_ptr.h> |
| 16 | #include <base/string_util.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 17 | #include <google/protobuf/repeated_field.h> |
| 18 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 19 | #include "update_engine/bzip_extent_writer.h" |
| 20 | #include "update_engine/delta_diff_generator.h" |
| 21 | #include "update_engine/extent_writer.h" |
| 22 | #include "update_engine/graph_types.h" |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 23 | #include "update_engine/payload_signer.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 24 | #include "update_engine/prefs_interface.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 25 | #include "update_engine/subprocess.h" |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 26 | #include "update_engine/terminator.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 27 | |
| 28 | using std::min; |
| 29 | using std::string; |
| 30 | using std::vector; |
| 31 | using google::protobuf::RepeatedPtrField; |
| 32 | |
| 33 | namespace chromeos_update_engine { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | const int kDeltaVersionLength = 8; |
| 38 | const int kDeltaProtobufLengthLength = 8; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 39 | const char kUpdatePayloadPublicKeyPath[] = |
| 40 | "/usr/share/update_engine/update-payload-key.pub.pem"; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 41 | const int kUpdateStateOperationInvalid = -1; |
| 42 | |
| 43 | // Returns true if |op| is idempotent -- i.e., if we can interrupt it and repeat |
| 44 | // it safely. Returns false otherwise. |
| 45 | bool IsIdempotentOperation(const DeltaArchiveManifest_InstallOperation& op) { |
| 46 | if (op.src_extents_size() == 0) { |
| 47 | return true; |
| 48 | } |
| 49 | // TODO(petkov): Cover the case where the source and target extents don't |
| 50 | // intersect. |
| 51 | return false; |
| 52 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 53 | |
| 54 | // Converts extents to a human-readable string, for use by DumpUpdateProto(). |
| 55 | string ExtentsToString(const RepeatedPtrField<Extent>& extents) { |
| 56 | string ret; |
| 57 | for (int i = 0; i < extents.size(); i++) { |
| 58 | const Extent& extent = extents.Get(i); |
| 59 | if (extent.start_block() == kSparseHole) { |
| 60 | ret += StringPrintf("{kSparseHole, %" PRIu64 "}, ", extent.num_blocks()); |
| 61 | } else { |
| 62 | ret += StringPrintf("{%" PRIu64 ", %" PRIu64 "}, ", |
| 63 | extent.start_block(), extent.num_blocks()); |
| 64 | } |
| 65 | } |
| 66 | if (!ret.empty()) { |
| 67 | DCHECK_GT(ret.size(), static_cast<size_t>(1)); |
| 68 | ret.resize(ret.size() - 2); |
| 69 | } |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | // LOGs a DeltaArchiveManifest object. Useful for debugging. |
| 74 | void DumpUpdateProto(const DeltaArchiveManifest& manifest) { |
| 75 | LOG(INFO) << "Update Proto:"; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 76 | LOG(INFO) << " block_size: " << manifest.block_size(); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 77 | for (int i = 0; i < (manifest.install_operations_size() + |
| 78 | manifest.kernel_install_operations_size()); i++) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 79 | const DeltaArchiveManifest_InstallOperation& op = |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 80 | i < manifest.install_operations_size() ? |
| 81 | manifest.install_operations(i) : |
| 82 | manifest.kernel_install_operations( |
| 83 | i - manifest.install_operations_size()); |
| 84 | if (i == 0) |
| 85 | LOG(INFO) << " Rootfs ops:"; |
| 86 | else if (i == manifest.install_operations_size()) |
| 87 | LOG(INFO) << " Kernel ops:"; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 88 | LOG(INFO) << " operation(" << i << ")"; |
| 89 | LOG(INFO) << " type: " |
| 90 | << DeltaArchiveManifest_InstallOperation_Type_Name(op.type()); |
| 91 | if (op.has_data_offset()) |
| 92 | LOG(INFO) << " data_offset: " << op.data_offset(); |
| 93 | if (op.has_data_length()) |
| 94 | LOG(INFO) << " data_length: " << op.data_length(); |
| 95 | LOG(INFO) << " src_extents: " << ExtentsToString(op.src_extents()); |
| 96 | if (op.has_src_length()) |
| 97 | LOG(INFO) << " src_length: " << op.src_length(); |
| 98 | LOG(INFO) << " dst_extents: " << ExtentsToString(op.dst_extents()); |
| 99 | if (op.has_dst_length()) |
| 100 | LOG(INFO) << " dst_length: " << op.dst_length(); |
| 101 | } |
| 102 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 103 | |
| 104 | // Opens path for read/write, put the fd into *fd. On success returns true |
| 105 | // and sets *err to 0. On failure, returns false and sets *err to errno. |
| 106 | bool OpenFile(const char* path, int* fd, int* err) { |
| 107 | if (*fd != -1) { |
| 108 | LOG(ERROR) << "Can't open(" << path << "), *fd != -1 (it's " << *fd << ")"; |
| 109 | *err = EINVAL; |
| 110 | return false; |
| 111 | } |
| 112 | *fd = open(path, O_RDWR, 000); |
| 113 | if (*fd < 0) { |
| 114 | *err = errno; |
| 115 | PLOG(ERROR) << "Unable to open file " << path; |
| 116 | return false; |
| 117 | } |
| 118 | *err = 0; |
| 119 | return true; |
| 120 | } |
| 121 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 122 | } // namespace {} |
| 123 | |
| 124 | int DeltaPerformer::Open(const char* path, int flags, mode_t mode) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 125 | int err; |
| 126 | if (OpenFile(path, &fd_, &err)) |
| 127 | path_ = path; |
| 128 | return -err; |
| 129 | } |
| 130 | |
| 131 | bool DeltaPerformer::OpenKernel(const char* kernel_path) { |
| 132 | int err; |
| 133 | bool success = OpenFile(kernel_path, &kernel_fd_, &err); |
| 134 | if (success) |
| 135 | kernel_path_ = kernel_path; |
| 136 | return success; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int DeltaPerformer::Close() { |
| 140 | if (!buffer_.empty()) { |
| 141 | LOG(ERROR) << "Called Close() while buffer not empty!"; |
| 142 | return -1; |
| 143 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 144 | int err = 0; |
| 145 | if (close(kernel_fd_) == -1) { |
| 146 | err = errno; |
| 147 | PLOG(ERROR) << "Unable to close kernel fd:"; |
| 148 | } |
| 149 | if (close(fd_) == -1) { |
| 150 | err = errno; |
| 151 | PLOG(ERROR) << "Unable to close rootfs fd:"; |
| 152 | } |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 153 | LOG_IF(ERROR, !hash_calculator_.Finalize()) << "Unable to finalize the hash."; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 154 | fd_ = -2; // Set so that isn't not valid AND calls to Open() will fail. |
| 155 | path_ = ""; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 156 | return -err; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Wrapper around write. Returns bytes written on success or |
| 160 | // -errno on error. |
| 161 | // This function performs as many actions as it can, given the amount of |
| 162 | // data received thus far. |
Andrew de los Reyes | 0cca421 | 2010-04-29 14:00:58 -0700 | [diff] [blame] | 163 | ssize_t DeltaPerformer::Write(const void* bytes, size_t count) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 164 | const char* c_bytes = reinterpret_cast<const char*>(bytes); |
| 165 | buffer_.insert(buffer_.end(), c_bytes, c_bytes + count); |
| 166 | |
| 167 | if (!manifest_valid_) { |
| 168 | // See if we have enough bytes for the manifest yet |
| 169 | if (buffer_.size() < strlen(kDeltaMagic) + |
| 170 | kDeltaVersionLength + kDeltaProtobufLengthLength) { |
| 171 | // Don't have enough bytes to even know the protobuf length |
| 172 | return count; |
| 173 | } |
| 174 | uint64_t protobuf_length; |
| 175 | COMPILE_ASSERT(sizeof(protobuf_length) == kDeltaProtobufLengthLength, |
| 176 | protobuf_length_size_mismatch); |
| 177 | memcpy(&protobuf_length, |
| 178 | &buffer_[strlen(kDeltaMagic) + kDeltaVersionLength], |
| 179 | kDeltaProtobufLengthLength); |
| 180 | protobuf_length = be64toh(protobuf_length); // switch big endian to host |
| 181 | if (buffer_.size() < strlen(kDeltaMagic) + kDeltaVersionLength + |
| 182 | kDeltaProtobufLengthLength + protobuf_length) { |
| 183 | return count; |
| 184 | } |
| 185 | // We have the full proto buffer in buffer_. Parse it. |
| 186 | const int offset = strlen(kDeltaMagic) + kDeltaVersionLength + |
| 187 | kDeltaProtobufLengthLength; |
| 188 | if (!manifest_.ParseFromArray(&buffer_[offset], protobuf_length)) { |
| 189 | LOG(ERROR) << "Unable to parse manifest in update file."; |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | // Remove protobuf and header info from buffer_, so buffer_ contains |
| 193 | // just data blobs |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 194 | manifest_metadata_size_ = strlen(kDeltaMagic) + kDeltaVersionLength + |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 195 | kDeltaProtobufLengthLength + protobuf_length; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 196 | DiscardBufferHeadBytes(manifest_metadata_size_); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 197 | LOG_IF(WARNING, !prefs_->SetInt64(kPrefsManifestMetadataSize, |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 198 | manifest_metadata_size_)) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 199 | << "Unable to save the manifest metadata size."; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 200 | manifest_valid_ = true; |
| 201 | block_size_ = manifest_.block_size(); |
| 202 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 203 | ssize_t total_operations = manifest_.install_operations_size() + |
| 204 | manifest_.kernel_install_operations_size(); |
| 205 | while (next_operation_num_ < total_operations) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 206 | const DeltaArchiveManifest_InstallOperation &op = |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 207 | next_operation_num_ < manifest_.install_operations_size() ? |
| 208 | manifest_.install_operations(next_operation_num_) : |
| 209 | manifest_.kernel_install_operations( |
| 210 | next_operation_num_ - manifest_.install_operations_size()); |
| 211 | if (!CanPerformInstallOperation(op)) |
| 212 | break; |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 213 | ScopedTerminatorExitUnblocker exit_unblocker = |
| 214 | ScopedTerminatorExitUnblocker(); // Avoids a compiler unused var bug. |
Andrew de los Reyes | bef0c7d | 2010-08-20 10:20:10 -0700 | [diff] [blame] | 215 | // Log every thousandth operation, and also the first and last ones |
| 216 | if ((next_operation_num_ % 1000 == 0) || |
| 217 | (next_operation_num_ + 1 == total_operations)) { |
| 218 | LOG(INFO) << "Performing operation " << (next_operation_num_ + 1) << "/" |
| 219 | << total_operations; |
| 220 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 221 | bool is_kernel_partition = |
| 222 | (next_operation_num_ >= manifest_.install_operations_size()); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 223 | // If about to start a non-idempotent operation, clear the update state so |
| 224 | // that if the operation gets interrupted, we don't try to resume the |
| 225 | // update. |
| 226 | if (!IsIdempotentOperation(op)) { |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 227 | Terminator::set_exit_blocked(true); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 228 | ResetUpdateProgress(prefs_); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 229 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 230 | if (op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE || |
| 231 | op.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 232 | if (!PerformReplaceOperation(op, is_kernel_partition)) { |
| 233 | LOG(ERROR) << "Failed to perform replace operation " |
| 234 | << next_operation_num_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 235 | return -EINVAL; |
| 236 | } |
| 237 | } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 238 | if (!PerformMoveOperation(op, is_kernel_partition)) { |
| 239 | LOG(ERROR) << "Failed to perform move operation " |
| 240 | << next_operation_num_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 241 | return -EINVAL; |
| 242 | } |
| 243 | } else if (op.type() == DeltaArchiveManifest_InstallOperation_Type_BSDIFF) { |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 244 | if (!PerformBsdiffOperation(op, is_kernel_partition)) { |
| 245 | LOG(ERROR) << "Failed to perform bsdiff operation " |
| 246 | << next_operation_num_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 247 | return -EINVAL; |
| 248 | } |
| 249 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 250 | next_operation_num_++; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 251 | CheckpointUpdateProgress(); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 252 | } |
| 253 | return count; |
| 254 | } |
| 255 | |
| 256 | bool DeltaPerformer::CanPerformInstallOperation( |
| 257 | const chromeos_update_engine::DeltaArchiveManifest_InstallOperation& |
| 258 | operation) { |
| 259 | // Move operations don't require any data blob, so they can always |
| 260 | // be performed |
| 261 | if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_MOVE) |
| 262 | return true; |
| 263 | |
| 264 | // See if we have the entire data blob in the buffer |
| 265 | if (operation.data_offset() < buffer_offset_) { |
| 266 | LOG(ERROR) << "we threw away data it seems?"; |
| 267 | return false; |
| 268 | } |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 269 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 270 | return (operation.data_offset() + operation.data_length()) <= |
| 271 | (buffer_offset_ + buffer_.size()); |
| 272 | } |
| 273 | |
| 274 | bool DeltaPerformer::PerformReplaceOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 275 | const DeltaArchiveManifest_InstallOperation& operation, |
| 276 | bool is_kernel_partition) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 277 | CHECK(operation.type() == \ |
| 278 | DeltaArchiveManifest_InstallOperation_Type_REPLACE || \ |
| 279 | operation.type() == \ |
| 280 | DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ); |
| 281 | |
| 282 | // Since we delete data off the beginning of the buffer as we use it, |
| 283 | // the data we need should be exactly at the beginning of the buffer. |
| 284 | CHECK_EQ(buffer_offset_, operation.data_offset()); |
| 285 | CHECK_GE(buffer_.size(), operation.data_length()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 286 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 287 | // Extract the signature message if it's in this operation. |
| 288 | ExtractSignatureMessage(operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 289 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 290 | DirectExtentWriter direct_writer; |
| 291 | ZeroPadExtentWriter zero_pad_writer(&direct_writer); |
| 292 | scoped_ptr<BzipExtentWriter> bzip_writer; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 293 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 294 | // Since bzip decompression is optional, we have a variable writer that will |
| 295 | // point to one of the ExtentWriter objects above. |
| 296 | ExtentWriter* writer = NULL; |
| 297 | if (operation.type() == DeltaArchiveManifest_InstallOperation_Type_REPLACE) { |
| 298 | writer = &zero_pad_writer; |
| 299 | } else if (operation.type() == |
| 300 | DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) { |
| 301 | bzip_writer.reset(new BzipExtentWriter(&zero_pad_writer)); |
| 302 | writer = bzip_writer.get(); |
| 303 | } else { |
| 304 | NOTREACHED(); |
| 305 | } |
| 306 | |
| 307 | // Create a vector of extents to pass to the ExtentWriter. |
| 308 | vector<Extent> extents; |
| 309 | for (int i = 0; i < operation.dst_extents_size(); i++) { |
| 310 | extents.push_back(operation.dst_extents(i)); |
| 311 | } |
| 312 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 313 | int fd = is_kernel_partition ? kernel_fd_ : fd_; |
| 314 | |
| 315 | TEST_AND_RETURN_FALSE(writer->Init(fd, extents, block_size_)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 316 | TEST_AND_RETURN_FALSE(writer->Write(&buffer_[0], operation.data_length())); |
| 317 | TEST_AND_RETURN_FALSE(writer->End()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 318 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 319 | // Update buffer |
| 320 | buffer_offset_ += operation.data_length(); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 321 | DiscardBufferHeadBytes(operation.data_length()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
| 325 | bool DeltaPerformer::PerformMoveOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 326 | const DeltaArchiveManifest_InstallOperation& operation, |
| 327 | bool is_kernel_partition) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 328 | // Calculate buffer size. Note, this function doesn't do a sliding |
| 329 | // window to copy in case the source and destination blocks overlap. |
| 330 | // If we wanted to do a sliding window, we could program the server |
| 331 | // to generate deltas that effectively did a sliding window. |
| 332 | |
| 333 | uint64_t blocks_to_read = 0; |
| 334 | for (int i = 0; i < operation.src_extents_size(); i++) |
| 335 | blocks_to_read += operation.src_extents(i).num_blocks(); |
| 336 | |
| 337 | uint64_t blocks_to_write = 0; |
| 338 | for (int i = 0; i < operation.dst_extents_size(); i++) |
| 339 | blocks_to_write += operation.dst_extents(i).num_blocks(); |
| 340 | |
| 341 | DCHECK_EQ(blocks_to_write, blocks_to_read); |
| 342 | vector<char> buf(blocks_to_write * block_size_); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 343 | |
| 344 | int fd = is_kernel_partition ? kernel_fd_ : fd_; |
| 345 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 346 | // Read in bytes. |
| 347 | ssize_t bytes_read = 0; |
| 348 | for (int i = 0; i < operation.src_extents_size(); i++) { |
| 349 | ssize_t bytes_read_this_iteration = 0; |
| 350 | const Extent& extent = operation.src_extents(i); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 351 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 352 | &buf[bytes_read], |
| 353 | extent.num_blocks() * block_size_, |
| 354 | extent.start_block() * block_size_, |
| 355 | &bytes_read_this_iteration)); |
| 356 | TEST_AND_RETURN_FALSE( |
| 357 | bytes_read_this_iteration == |
| 358 | static_cast<ssize_t>(extent.num_blocks() * block_size_)); |
| 359 | bytes_read += bytes_read_this_iteration; |
| 360 | } |
| 361 | |
| 362 | // Write bytes out. |
| 363 | ssize_t bytes_written = 0; |
| 364 | for (int i = 0; i < operation.dst_extents_size(); i++) { |
| 365 | const Extent& extent = operation.dst_extents(i); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 366 | TEST_AND_RETURN_FALSE(utils::PWriteAll(fd, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 367 | &buf[bytes_written], |
| 368 | extent.num_blocks() * block_size_, |
| 369 | extent.start_block() * block_size_)); |
| 370 | bytes_written += extent.num_blocks() * block_size_; |
| 371 | } |
| 372 | DCHECK_EQ(bytes_written, bytes_read); |
| 373 | DCHECK_EQ(bytes_written, static_cast<ssize_t>(buf.size())); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | bool DeltaPerformer::ExtentsToBsdiffPositionsString( |
| 378 | const RepeatedPtrField<Extent>& extents, |
| 379 | uint64_t block_size, |
| 380 | uint64_t full_length, |
| 381 | string* positions_string) { |
| 382 | string ret; |
| 383 | uint64_t length = 0; |
| 384 | for (int i = 0; i < extents.size(); i++) { |
| 385 | Extent extent = extents.Get(i); |
| 386 | int64_t start = extent.start_block(); |
| 387 | uint64_t this_length = min(full_length - length, |
| 388 | extent.num_blocks() * block_size); |
| 389 | if (start == static_cast<int64_t>(kSparseHole)) |
| 390 | start = -1; |
| 391 | else |
| 392 | start *= block_size; |
| 393 | ret += StringPrintf("%" PRIi64 ":%" PRIu64 ",", start, this_length); |
| 394 | length += this_length; |
| 395 | } |
| 396 | TEST_AND_RETURN_FALSE(length == full_length); |
| 397 | if (!ret.empty()) |
| 398 | ret.resize(ret.size() - 1); // Strip trailing comma off |
| 399 | *positions_string = ret; |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | bool DeltaPerformer::PerformBsdiffOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 404 | const DeltaArchiveManifest_InstallOperation& operation, |
| 405 | bool is_kernel_partition) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 406 | // Since we delete data off the beginning of the buffer as we use it, |
| 407 | // the data we need should be exactly at the beginning of the buffer. |
| 408 | CHECK_EQ(buffer_offset_, operation.data_offset()); |
| 409 | CHECK_GE(buffer_.size(), operation.data_length()); |
| 410 | |
| 411 | string input_positions; |
| 412 | TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.src_extents(), |
| 413 | block_size_, |
| 414 | operation.src_length(), |
| 415 | &input_positions)); |
| 416 | string output_positions; |
| 417 | TEST_AND_RETURN_FALSE(ExtentsToBsdiffPositionsString(operation.dst_extents(), |
| 418 | block_size_, |
| 419 | operation.dst_length(), |
| 420 | &output_positions)); |
| 421 | |
| 422 | string temp_filename; |
| 423 | TEST_AND_RETURN_FALSE(utils::MakeTempFile("/tmp/au_patch.XXXXXX", |
| 424 | &temp_filename, |
| 425 | NULL)); |
| 426 | ScopedPathUnlinker path_unlinker(temp_filename); |
| 427 | { |
| 428 | int fd = open(temp_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 429 | ScopedFdCloser fd_closer(&fd); |
| 430 | TEST_AND_RETURN_FALSE( |
| 431 | utils::WriteAll(fd, &buffer_[0], operation.data_length())); |
| 432 | } |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 433 | |
| 434 | int fd = is_kernel_partition ? kernel_fd_ : fd_; |
| 435 | const string& path = is_kernel_partition ? kernel_path_ : path_; |
| 436 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 437 | vector<string> cmd; |
| 438 | cmd.push_back(kBspatchPath); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 439 | cmd.push_back(path); |
| 440 | cmd.push_back(path); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 441 | cmd.push_back(temp_filename); |
| 442 | cmd.push_back(input_positions); |
| 443 | cmd.push_back(output_positions); |
| 444 | int return_code = 0; |
| 445 | TEST_AND_RETURN_FALSE(Subprocess::SynchronousExec(cmd, &return_code)); |
| 446 | TEST_AND_RETURN_FALSE(return_code == 0); |
| 447 | |
| 448 | if (operation.dst_length() % block_size_) { |
| 449 | // Zero out rest of final block. |
| 450 | // TODO(adlr): build this into bspatch; it's more efficient that way. |
| 451 | const Extent& last_extent = |
| 452 | operation.dst_extents(operation.dst_extents_size() - 1); |
| 453 | const uint64_t end_byte = |
| 454 | (last_extent.start_block() + last_extent.num_blocks()) * block_size_; |
| 455 | const uint64_t begin_byte = |
| 456 | end_byte - (block_size_ - operation.dst_length() % block_size_); |
| 457 | vector<char> zeros(end_byte - begin_byte); |
| 458 | TEST_AND_RETURN_FALSE( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 459 | utils::PWriteAll(fd, &zeros[0], end_byte - begin_byte, begin_byte)); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // Update buffer. |
| 463 | buffer_offset_ += operation.data_length(); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 464 | DiscardBufferHeadBytes(operation.data_length()); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 465 | return true; |
| 466 | } |
| 467 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 468 | bool DeltaPerformer::ExtractSignatureMessage( |
| 469 | const DeltaArchiveManifest_InstallOperation& operation) { |
| 470 | if (operation.type() != DeltaArchiveManifest_InstallOperation_Type_REPLACE || |
| 471 | !manifest_.has_signatures_offset() || |
| 472 | manifest_.signatures_offset() != operation.data_offset()) { |
| 473 | return false; |
| 474 | } |
| 475 | TEST_AND_RETURN_FALSE(manifest_.has_signatures_size() && |
| 476 | manifest_.signatures_size() == operation.data_length()); |
| 477 | TEST_AND_RETURN_FALSE(signatures_message_data_.empty()); |
| 478 | TEST_AND_RETURN_FALSE(buffer_offset_ == manifest_.signatures_offset()); |
| 479 | TEST_AND_RETURN_FALSE(buffer_.size() >= manifest_.signatures_size()); |
| 480 | signatures_message_data_.insert( |
| 481 | signatures_message_data_.begin(), |
| 482 | buffer_.begin(), |
| 483 | buffer_.begin() + manifest_.signatures_size()); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 484 | // The hash of all data consumed so far should be verified against the signed |
| 485 | // hash. |
| 486 | signed_hash_context_ = hash_calculator_.GetContext(); |
| 487 | LOG_IF(WARNING, !prefs_->SetString(kPrefsUpdateStateSignedSHA256Context, |
| 488 | signed_hash_context_)) |
| 489 | << "Unable to store the signed hash context."; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 490 | LOG(INFO) << "Extracted signature data of size " |
| 491 | << manifest_.signatures_size() << " at " |
| 492 | << manifest_.signatures_offset(); |
| 493 | return true; |
| 494 | } |
| 495 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 496 | bool DeltaPerformer::VerifyPayload( |
| 497 | const string& public_key_path, |
| 498 | const std::string& update_check_response_hash, |
| 499 | const uint64_t update_check_response_size) { |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 500 | string key_path = public_key_path; |
| 501 | if (key_path.empty()) { |
| 502 | key_path = kUpdatePayloadPublicKeyPath; |
| 503 | } |
| 504 | LOG(INFO) << "Verifying delta payload. Public key path: " << key_path; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 505 | |
| 506 | // Verifies the download hash. |
| 507 | const string& download_hash_data = hash_calculator_.hash(); |
| 508 | TEST_AND_RETURN_FALSE(!download_hash_data.empty()); |
| 509 | TEST_AND_RETURN_FALSE(download_hash_data == update_check_response_hash); |
| 510 | |
| 511 | // Verifies the download size. |
| 512 | TEST_AND_RETURN_FALSE(update_check_response_size == |
| 513 | manifest_metadata_size_ + buffer_offset_); |
| 514 | |
| 515 | // Verifies the signed payload hash. |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 516 | if (!utils::FileExists(key_path.c_str())) { |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 517 | LOG(WARNING) << "Not verifying signed delta payload -- missing public key."; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 518 | return true; |
| 519 | } |
| 520 | TEST_AND_RETURN_FALSE(!signatures_message_data_.empty()); |
| 521 | vector<char> signed_hash_data; |
| 522 | TEST_AND_RETURN_FALSE(PayloadSigner::VerifySignature(signatures_message_data_, |
| 523 | key_path, |
| 524 | &signed_hash_data)); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 525 | OmahaHashCalculator signed_hasher; |
| 526 | // TODO(petkov): Make sure signed_hash_context_ is loaded when resuming an |
| 527 | // update. |
| 528 | TEST_AND_RETURN_FALSE(signed_hasher.SetContext(signed_hash_context_)); |
| 529 | TEST_AND_RETURN_FALSE(signed_hasher.Finalize()); |
| 530 | const vector<char>& hash_data = signed_hasher.raw_hash(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 531 | TEST_AND_RETURN_FALSE(!hash_data.empty()); |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 532 | TEST_AND_RETURN_FALSE(hash_data == signed_hash_data); |
| 533 | return true; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 536 | void DeltaPerformer::DiscardBufferHeadBytes(size_t count) { |
| 537 | hash_calculator_.Update(&buffer_[0], count); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 538 | buffer_.erase(buffer_.begin(), buffer_.begin() + count); |
| 539 | } |
| 540 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 541 | bool DeltaPerformer::CanResumeUpdate(PrefsInterface* prefs, |
| 542 | string update_check_response_hash) { |
| 543 | int64_t next_operation = kUpdateStateOperationInvalid; |
| 544 | TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextOperation, |
| 545 | &next_operation) && |
| 546 | next_operation != kUpdateStateOperationInvalid && |
| 547 | next_operation > 0); |
| 548 | |
| 549 | string interrupted_hash; |
| 550 | TEST_AND_RETURN_FALSE(prefs->GetString(kPrefsUpdateCheckResponseHash, |
| 551 | &interrupted_hash) && |
| 552 | !interrupted_hash.empty() && |
| 553 | interrupted_hash == update_check_response_hash); |
| 554 | |
| 555 | // Sanity check the rest. |
| 556 | int64_t next_data_offset = -1; |
| 557 | TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsUpdateStateNextDataOffset, |
| 558 | &next_data_offset) && |
| 559 | next_data_offset >= 0); |
| 560 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 561 | string sha256_context; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 562 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 563 | prefs->GetString(kPrefsUpdateStateSHA256Context, &sha256_context) && |
| 564 | !sha256_context.empty()); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 565 | |
| 566 | int64_t manifest_metadata_size = 0; |
| 567 | TEST_AND_RETURN_FALSE(prefs->GetInt64(kPrefsManifestMetadataSize, |
| 568 | &manifest_metadata_size) && |
| 569 | manifest_metadata_size > 0); |
| 570 | |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | bool DeltaPerformer::ResetUpdateProgress(PrefsInterface* prefs) { |
| 575 | TEST_AND_RETURN_FALSE(prefs->SetInt64(kPrefsUpdateStateNextOperation, |
| 576 | kUpdateStateOperationInvalid)); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 577 | return true; |
| 578 | } |
| 579 | |
| 580 | bool DeltaPerformer::CheckpointUpdateProgress() { |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 581 | Terminator::set_exit_blocked(true); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 582 | if (last_updated_buffer_offset_ != buffer_offset_) { |
Darin Petkov | 9c0baf8 | 2010-10-07 13:44:48 -0700 | [diff] [blame] | 583 | // Resets the progress in case we die in the middle of the state update. |
| 584 | ResetUpdateProgress(prefs_); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 585 | TEST_AND_RETURN_FALSE( |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 586 | prefs_->SetString(kPrefsUpdateStateSHA256Context, |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 587 | hash_calculator_.GetContext())); |
| 588 | TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextDataOffset, |
| 589 | buffer_offset_)); |
| 590 | last_updated_buffer_offset_ = buffer_offset_; |
| 591 | } |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 592 | TEST_AND_RETURN_FALSE(prefs_->SetInt64(kPrefsUpdateStateNextOperation, |
| 593 | next_operation_num_)); |
| 594 | return true; |
| 595 | } |
| 596 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 597 | } // namespace chromeos_update_engine |