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