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