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 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |
| 7 | |
| 8 | #include <inttypes.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 <vector> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 11 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 12 | #include <google/protobuf/repeated_field.h> |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 14 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 15 | #include "update_engine/file_writer.h" |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 16 | #include "update_engine/omaha_hash_calculator.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 17 | #include "update_engine/update_metadata.pb.h" |
| 18 | |
| 19 | namespace chromeos_update_engine { |
| 20 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 21 | class PrefsInterface; |
| 22 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 23 | // This class performs the actions in a delta update synchronously. The delta |
| 24 | // update itself should be passed in in chunks as it is received. |
| 25 | |
| 26 | class DeltaPerformer : public FileWriter { |
| 27 | public: |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 28 | enum MetadataParseResult { |
| 29 | kMetadataParseSuccess, |
| 30 | kMetadataParseError, |
| 31 | kMetadataParseInsufficientData, |
| 32 | }; |
| 33 | |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 34 | static const char kUpdatePayloadPublicKeyPath[]; |
| 35 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 36 | DeltaPerformer(PrefsInterface* prefs) |
| 37 | : prefs_(prefs), |
| 38 | fd_(-1), |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 39 | kernel_fd_(-1), |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | manifest_valid_(false), |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 41 | manifest_metadata_size_(0), |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 42 | next_operation_num_(0), |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 43 | buffer_offset_(0), |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 44 | last_updated_buffer_offset_(kuint64max), |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 45 | block_size_(0) {} |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 46 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 47 | // Opens the kernel. Should be called before or after Open(), but before |
| 48 | // Write(). The kernel file will be close()d when Close() is called. |
| 49 | bool OpenKernel(const char* kernel_path); |
| 50 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 51 | // flags and mode ignored. Once Close()d, a DeltaPerformer can't be |
| 52 | // Open()ed again. |
| 53 | int Open(const char* path, int flags, mode_t mode); |
| 54 | |
Don Garrett | e410e0f | 2011-11-10 15:39:01 -0800 | [diff] [blame] | 55 | // Wrapper around write. Returns true if all requested bytes |
| 56 | // were written, or false on any error, reguardless of progress. |
| 57 | bool Write(const void* bytes, size_t count); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 58 | |
| 59 | // Wrapper around close. Returns 0 on success or -errno on error. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 60 | // Closes both 'path' given to Open() and the kernel path. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 61 | int Close(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 62 | |
| 63 | // Verifies the downloaded payload against the signed hash included in the |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 64 | // payload, against the update check hash and size, and against the public |
| 65 | // key and returns kActionCodeSuccess on success, an error code on failure. |
| 66 | // This method should be called after closing the stream. Note this method |
| 67 | // skips the signed hash check if the public key is unavailable; it returns |
| 68 | // kActionCodeSignedDeltaPayloadExpectedError if the public key |
| 69 | // is available but the delta payload doesn't include a signature. If |
| 70 | // |public_key_path| is an empty string, uses the default public key path. |
| 71 | ActionExitCode VerifyPayload(const std::string& public_key_path, |
| 72 | const std::string& update_check_response_hash, |
| 73 | const uint64_t update_check_response_size); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 74 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 75 | // Reads from the update manifest the expected sizes and hashes of the target |
| 76 | // kernel and rootfs partitions. These values can be used for applied update |
| 77 | // hash verification. This method must be called after the update manifest has |
| 78 | // been parsed (e.g., after closing the stream). Returns true on success, and |
| 79 | // false on failure (e.g., when the values are not present in the update |
| 80 | // manifest). |
| 81 | bool GetNewPartitionInfo(uint64_t* kernel_size, |
| 82 | std::vector<char>* kernel_hash, |
| 83 | uint64_t* rootfs_size, |
| 84 | std::vector<char>* rootfs_hash); |
Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 85 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 86 | // Converts an ordered collection of Extent objects which contain data of |
| 87 | // length full_length to a comma-separated string. For each Extent, the |
| 88 | // string will have the start offset and then the length in bytes. |
| 89 | // The length value of the last extent in the string may be short, since |
| 90 | // the full length of all extents in the string is capped to full_length. |
| 91 | // Also, an extent starting at kSparseHole, appears as -1 in the string. |
| 92 | // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1}, |
| 93 | // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13, |
| 94 | // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083" |
| 95 | static bool ExtentsToBsdiffPositionsString( |
| 96 | const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 97 | uint64_t block_size, |
| 98 | uint64_t full_length, |
| 99 | std::string* positions_string); |
| 100 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 101 | // Returns true if a previous update attempt can be continued based on the |
| 102 | // persistent preferences and the new update check response hash. |
| 103 | static bool CanResumeUpdate(PrefsInterface* prefs, |
| 104 | std::string update_check_response_hash); |
| 105 | |
| 106 | // Resets the persistent update progress state to indicate that an update |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 107 | // can't be resumed. Performs a quick update-in-progress reset if |quick| is |
| 108 | // true, otherwise resets all progress-related update state. Returns true on |
| 109 | // success, false otherwise. |
| 110 | static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 111 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 112 | // Attempts to parse the update metadata starting from the beginning of |
| 113 | // |payload| into |manifest|. On success, sets |metadata_size| to the total |
| 114 | // metadata bytes (including the delta magic and metadata size fields), and |
| 115 | // returns kMetadataParseSuccess. Returns kMetadataParseInsufficientData if |
| 116 | // more data is needed to parse the complete metadata. Returns |
| 117 | // kMetadataParseError if the metadata can't be parsed given the payload. |
| 118 | static MetadataParseResult ParsePayloadMetadata( |
| 119 | const std::vector<char>& payload, |
| 120 | DeltaArchiveManifest* manifest, |
| 121 | uint64_t* metadata_size); |
| 122 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 123 | void set_current_kernel_hash(const std::vector<char>& hash) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 124 | current_kernel_hash_ = hash; |
| 125 | } |
| 126 | |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 127 | void set_current_rootfs_hash(const std::vector<char>& hash) { |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 128 | current_rootfs_hash_ = hash; |
| 129 | } |
| 130 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 131 | private: |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 132 | friend class DeltaPerformerTest; |
| 133 | FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest); |
| 134 | |
| 135 | static bool IsIdempotentOperation( |
| 136 | const DeltaArchiveManifest_InstallOperation& op); |
| 137 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 138 | // Verifies that the expected source partition hashes (if present) match the |
| 139 | // hashes for the current partitions. Returns true if there're no expected |
| 140 | // hashes in the payload (e.g., if it's a new-style full update) or if the |
| 141 | // hashes match; returns false otherwise. |
| 142 | bool VerifySourcePartitions(); |
| 143 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 144 | // Returns true if enough of the delta file has been passed via Write() |
| 145 | // to be able to perform a given install operation. |
| 146 | bool CanPerformInstallOperation( |
| 147 | const DeltaArchiveManifest_InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 148 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 149 | // Returns true on success. |
| 150 | bool PerformInstallOperation( |
| 151 | const DeltaArchiveManifest_InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 152 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 153 | // These perform a specific type of operation and return true on success. |
| 154 | bool PerformReplaceOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 155 | const DeltaArchiveManifest_InstallOperation& operation, |
| 156 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 157 | bool PerformMoveOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 158 | const DeltaArchiveManifest_InstallOperation& operation, |
| 159 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 160 | bool PerformBsdiffOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 161 | const DeltaArchiveManifest_InstallOperation& operation, |
| 162 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 163 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 164 | // Returns true if the payload signature message has been extracted from |
| 165 | // |operation|, false otherwise. |
| 166 | bool ExtractSignatureMessage( |
| 167 | const DeltaArchiveManifest_InstallOperation& operation); |
| 168 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 169 | // Updates the hash calculator with |count| bytes at the head of |buffer_| and |
| 170 | // then discards them. |
| 171 | void DiscardBufferHeadBytes(size_t count); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 172 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 173 | // Checkpoints the update progress into persistent storage to allow this |
| 174 | // update attempt to be resumed after reboot. |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 175 | bool CheckpointUpdateProgress(); |
| 176 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 177 | // Primes the required update state. Returns true if the update state was |
| 178 | // successfully initialized to a saved resume state or if the update is a new |
| 179 | // update. Returns false otherwise. |
| 180 | bool PrimeUpdateState(); |
| 181 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 182 | // Update Engine preference store. |
| 183 | PrefsInterface* prefs_; |
| 184 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 185 | // File descriptor of open device. |
| 186 | int fd_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 187 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 188 | // File descriptor of the kernel device |
| 189 | int kernel_fd_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 190 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 191 | std::string path_; // Path that fd_ refers to. |
| 192 | std::string kernel_path_; // Path that kernel_fd_ refers to. |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 193 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 194 | DeltaArchiveManifest manifest_; |
| 195 | bool manifest_valid_; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 196 | uint64_t manifest_metadata_size_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 197 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 198 | // Index of the next operation to perform in the manifest. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 199 | int next_operation_num_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 200 | |
| 201 | // buffer_ is a window of the data that's been downloaded. At first, |
| 202 | // it contains the beginning of the download, but after the protobuf |
| 203 | // has been downloaded and parsed, it contains a sliding window of |
| 204 | // data blobs. |
| 205 | std::vector<char> buffer_; |
| 206 | // Offset of buffer_ in the binary blobs section of the update. |
| 207 | uint64_t buffer_offset_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 208 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 209 | // Last |buffer_offset_| value updated as part of the progress update. |
| 210 | uint64_t last_updated_buffer_offset_; |
| 211 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 212 | // The block size (parsed from the manifest). |
| 213 | uint32_t block_size_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 214 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 215 | // Calculates the payload hash. |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 216 | OmahaHashCalculator hash_calculator_; |
| 217 | |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 218 | // Saves the signed hash context. |
| 219 | std::string signed_hash_context_; |
| 220 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 221 | // Signatures message blob extracted directly from the payload. |
| 222 | std::vector<char> signatures_message_data_; |
| 223 | |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 224 | // Hashes for the current partitions to be used for source partition |
| 225 | // verification. |
Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 226 | std::vector<char> current_kernel_hash_; |
| 227 | std::vector<char> current_rootfs_hash_; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 228 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 229 | DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); |
| 230 | }; |
| 231 | |
| 232 | } // namespace chromeos_update_engine |
| 233 | |
| 234 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |