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), |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 32 | next_operation_num_(0), |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 33 | buffer_offset_(0), |
| 34 | block_size_(0) {} |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 35 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 36 | // Opens the kernel. Should be called before or after Open(), but before |
| 37 | // Write(). The kernel file will be close()d when Close() is called. |
| 38 | bool OpenKernel(const char* kernel_path); |
| 39 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | // flags and mode ignored. Once Close()d, a DeltaPerformer can't be |
| 41 | // Open()ed again. |
| 42 | int Open(const char* path, int flags, mode_t mode); |
| 43 | |
| 44 | // Wrapper around write. Returns bytes written on success or |
| 45 | // -errno on error. |
Andrew de los Reyes | 0cca421 | 2010-04-29 14:00:58 -0700 | [diff] [blame] | 46 | ssize_t Write(const void* bytes, size_t count); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 47 | |
| 48 | // 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] | 49 | // Closes both 'path' given to Open() and the kernel path. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 50 | int Close(); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 51 | |
| 52 | // Verifies the downloaded payload against the signed hash included in the |
| 53 | // payload and returns true on success, false on failure. This method should |
| 54 | // be called after closing the stream. Note this method returns true if the |
| 55 | // public key is unavailable; it returns false if the public key is available |
| 56 | // but the delta payload doesn't include a signature. If |public_key_path| is |
| 57 | // an empty string, uses the default public key path. |
| 58 | bool VerifyPayload(const std::string& public_key_path); |
| 59 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 60 | // Converts an ordered collection of Extent objects which contain data of |
| 61 | // length full_length to a comma-separated string. For each Extent, the |
| 62 | // string will have the start offset and then the length in bytes. |
| 63 | // The length value of the last extent in the string may be short, since |
| 64 | // the full length of all extents in the string is capped to full_length. |
| 65 | // Also, an extent starting at kSparseHole, appears as -1 in the string. |
| 66 | // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1}, |
| 67 | // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13, |
| 68 | // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083" |
| 69 | static bool ExtentsToBsdiffPositionsString( |
| 70 | const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 71 | uint64_t block_size, |
| 72 | uint64_t full_length, |
| 73 | std::string* positions_string); |
| 74 | |
| 75 | private: |
| 76 | // Returns true if enough of the delta file has been passed via Write() |
| 77 | // to be able to perform a given install operation. |
| 78 | bool CanPerformInstallOperation( |
| 79 | const DeltaArchiveManifest_InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 80 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 81 | // Returns true on success. |
| 82 | bool PerformInstallOperation( |
| 83 | const DeltaArchiveManifest_InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 84 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 85 | // These perform a specific type of operation and return true on success. |
| 86 | bool PerformReplaceOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 87 | const DeltaArchiveManifest_InstallOperation& operation, |
| 88 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 89 | bool PerformMoveOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 90 | const DeltaArchiveManifest_InstallOperation& operation, |
| 91 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 92 | bool PerformBsdiffOperation( |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 93 | const DeltaArchiveManifest_InstallOperation& operation, |
| 94 | bool is_kernel_partition); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 95 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 96 | // Returns true if the payload signature message has been extracted from |
| 97 | // |operation|, false otherwise. |
| 98 | bool ExtractSignatureMessage( |
| 99 | const DeltaArchiveManifest_InstallOperation& operation); |
| 100 | |
| 101 | // Discard |count| bytes from the beginning of buffer_. If |do_hash| is true, |
| 102 | // updates the hash calculator with these bytes before discarding them. |
| 103 | void DiscardBufferHeadBytes(size_t count, bool do_hash); |
| 104 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 105 | bool ResetUpdateProgress(); |
| 106 | bool CheckpointUpdateProgress(); |
| 107 | |
| 108 | // Update Engine preference store. |
| 109 | PrefsInterface* prefs_; |
| 110 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 111 | // File descriptor of open device. |
| 112 | int fd_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 113 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 114 | // File descriptor of the kernel device |
| 115 | int kernel_fd_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 116 | |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 117 | std::string path_; // Path that fd_ refers to. |
| 118 | std::string kernel_path_; // Path that kernel_fd_ refers to. |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 119 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 120 | DeltaArchiveManifest manifest_; |
| 121 | bool manifest_valid_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 122 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 123 | // Index of the next operation to perform in the manifest. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 124 | int next_operation_num_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 125 | |
| 126 | // buffer_ is a window of the data that's been downloaded. At first, |
| 127 | // it contains the beginning of the download, but after the protobuf |
| 128 | // has been downloaded and parsed, it contains a sliding window of |
| 129 | // data blobs. |
| 130 | std::vector<char> buffer_; |
| 131 | // Offset of buffer_ in the binary blobs section of the update. |
| 132 | uint64_t buffer_offset_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 133 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 134 | // The block size (parsed from the manifest). |
| 135 | uint32_t block_size_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 136 | |
| 137 | // Calculate the payload hash to verify against the signed hash. |
| 138 | OmahaHashCalculator hash_calculator_; |
| 139 | |
| 140 | // Signatures message blob extracted directly from the payload. |
| 141 | std::vector<char> signatures_message_data_; |
| 142 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 143 | DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); |
| 144 | }; |
| 145 | |
| 146 | } // namespace chromeos_update_engine |
| 147 | |
| 148 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |