| 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 |  | 
| Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_DELTA_PERFORMER_H_ | 
 | 6 | #define UPDATE_ENGINE_DELTA_PERFORMER_H_ | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 7 |  | 
 | 8 | #include <inttypes.h> | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 9 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 10 | #include <string> | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 11 | #include <vector> | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 12 |  | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 13 | #include <base/time/time.h> | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 14 | #include <chromeos/secure_blob.h> | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 15 | #include <google/protobuf/repeated_field.h> | 
| Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h>  // for FRIEND_TEST | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 17 |  | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 18 | #include "update_engine/file_descriptor.h" | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 19 | #include "update_engine/file_writer.h" | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 20 | #include "update_engine/install_plan.h" | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 21 | #include "update_engine/omaha_hash_calculator.h" | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 22 | #include "update_engine/system_state.h" | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 23 | #include "update_engine/update_metadata.pb.h" | 
 | 24 |  | 
 | 25 | namespace chromeos_update_engine { | 
 | 26 |  | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 27 | // The minor version used by the in-place delta generator algorithm. | 
 | 28 | extern const uint32_t kInPlaceMinorPayloadVersion; | 
 | 29 |  | 
 | 30 | // The minor version used by the A to B delta generator algorithm. | 
 | 31 | extern const uint32_t kSourceMinorPayloadVersion; | 
 | 32 |  | 
| Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 33 | // Chunk size used for payloads during test. | 
 | 34 | extern const size_t kDefaultChunkSize; | 
 | 35 |  | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 36 | class PrefsInterface; | 
 | 37 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 38 | // This class performs the actions in a delta update synchronously. The delta | 
 | 39 | // update itself should be passed in in chunks as it is received. | 
 | 40 |  | 
 | 41 | class DeltaPerformer : public FileWriter { | 
 | 42 |  public: | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 43 |   enum MetadataParseResult { | 
 | 44 |     kMetadataParseSuccess, | 
 | 45 |     kMetadataParseError, | 
 | 46 |     kMetadataParseInsufficientData, | 
 | 47 |   }; | 
 | 48 |  | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 49 |   static const uint64_t kDeltaVersionSize; | 
 | 50 |   static const uint64_t kDeltaManifestSizeSize; | 
| Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 51 |   static const uint64_t kSupportedMajorPayloadVersion; | 
| Don Garrett | b8dd1d9 | 2013-11-22 17:40:02 -0800 | [diff] [blame] | 52 |   static const uint64_t kSupportedMinorPayloadVersion; | 
 | 53 |   static const uint64_t kFullPayloadMinorVersion; | 
| Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 54 |   static const char kUpdatePayloadPublicKeyPath[]; | 
 | 55 |  | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 56 |   // Defines the granularity of progress logging in terms of how many "completed | 
 | 57 |   // chunks" we want to report at the most. | 
 | 58 |   static const unsigned kProgressLogMaxChunks; | 
 | 59 |   // Defines a timeout since the last progress was logged after which we want to | 
 | 60 |   // force another log message (even if the current chunk was not completed). | 
 | 61 |   static const unsigned kProgressLogTimeoutSeconds; | 
 | 62 |   // These define the relative weights (0-100) we give to the different work | 
 | 63 |   // components associated with an update when computing an overall progress. | 
 | 64 |   // Currently they include the download progress and the number of completed | 
 | 65 |   // operations. They must add up to one hundred (100). | 
 | 66 |   static const unsigned kProgressDownloadWeight; | 
 | 67 |   static const unsigned kProgressOperationsWeight; | 
 | 68 |  | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 69 |   DeltaPerformer(PrefsInterface* prefs, | 
 | 70 |                  SystemState* system_state, | 
 | 71 |                  InstallPlan* install_plan) | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 72 |       : prefs_(prefs), | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 73 |         system_state_(system_state), | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 74 |         install_plan_(install_plan), | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 75 |         fd_(nullptr), | 
 | 76 |         kernel_fd_(nullptr), | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 77 |         source_fd_(nullptr), | 
 | 78 |         source_kernel_fd_(nullptr), | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 79 |         manifest_parsed_(false), | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 80 |         manifest_valid_(false), | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 81 |         metadata_size_(0), | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 82 |         next_operation_num_(0), | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 83 |         buffer_offset_(0), | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 84 |         last_updated_buffer_offset_(kuint64max), | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 85 |         block_size_(0), | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 86 |         public_key_path_(kUpdatePayloadPublicKeyPath), | 
 | 87 |         total_bytes_received_(0), | 
 | 88 |         num_rootfs_operations_(0), | 
 | 89 |         num_total_operations_(0), | 
 | 90 |         overall_progress_(0), | 
 | 91 |         last_progress_chunk_(0), | 
 | 92 |         forced_progress_log_wait_( | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 93 |             base::TimeDelta::FromSeconds(kProgressLogTimeoutSeconds)), | 
 | 94 |         supported_minor_version_(kSupportedMinorPayloadVersion) {} | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 95 |  | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 96 |   // Opens the kernel. Should be called before or after Open(), but before | 
 | 97 |   // Write(). The kernel file will be close()d when Close() is called. | 
 | 98 |   bool OpenKernel(const char* kernel_path); | 
 | 99 |  | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 100 |   // Opens the source partition. The file will be closed when Close() is called. | 
 | 101 |   bool OpenSourceRootfs(const std::string& kernel_path); | 
 | 102 |  | 
 | 103 |   // Opens the source kernel. The file will be closed when Close() is called. | 
 | 104 |   bool OpenSourceKernel(const std::string& source_kernel_path); | 
 | 105 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 106 |   // flags and mode ignored. Once Close()d, a DeltaPerformer can't be | 
 | 107 |   // Open()ed again. | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 108 |   int Open(const char* path, int flags, mode_t mode) override; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 109 |  | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 110 |   // FileWriter's Write implementation where caller doesn't care about | 
 | 111 |   // error codes. | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 112 |   bool Write(const void* bytes, size_t count) override { | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 113 |     ErrorCode error; | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 114 |     return Write(bytes, count, &error); | 
 | 115 |   } | 
 | 116 |  | 
 | 117 |   // FileWriter's Write implementation that returns a more specific |error| code | 
 | 118 |   // in case of failures in Write operation. | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 119 |   bool Write(const void* bytes, size_t count, ErrorCode *error) override; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 120 |  | 
 | 121 |   // 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] | 122 |   // Closes both 'path' given to Open() and the kernel path. | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 123 |   int Close() override; | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 124 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 125 |   // Returns |true| only if the manifest has been processed and it's valid. | 
 | 126 |   bool IsManifestValid(); | 
 | 127 |  | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 128 |   // Verifies the downloaded payload against the signed hash included in the | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 129 |   // payload, against the update check hash (which is in base64 format)  and | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 130 |   // size using the public key and returns ErrorCode::kSuccess on success, an | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 131 |   // error code on failure.  This method should be called after closing the | 
 | 132 |   // stream. Note this method skips the signed hash check if the public key is | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 133 |   // unavailable; it returns ErrorCode::kSignedDeltaPayloadExpectedError if the | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 134 |   // public key is available but the delta payload doesn't include a signature. | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 135 |   ErrorCode VerifyPayload(const std::string& update_check_response_hash, | 
| Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 136 |                           const uint64_t update_check_response_size); | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 137 |  | 
| Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 138 |   // Reads from the update manifest the expected sizes and hashes of the target | 
 | 139 |   // kernel and rootfs partitions. These values can be used for applied update | 
 | 140 |   // hash verification. This method must be called after the update manifest has | 
 | 141 |   // been parsed (e.g., after closing the stream). Returns true on success, and | 
 | 142 |   // false on failure (e.g., when the values are not present in the update | 
 | 143 |   // manifest). | 
 | 144 |   bool GetNewPartitionInfo(uint64_t* kernel_size, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 145 |                            chromeos::Blob* kernel_hash, | 
| Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 146 |                            uint64_t* rootfs_size, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 147 |                            chromeos::Blob* rootfs_hash); | 
| Darin Petkov | 2dd0109 | 2010-10-08 15:43:05 -0700 | [diff] [blame] | 148 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 149 |   // Converts an ordered collection of Extent objects which contain data of | 
 | 150 |   // length full_length to a comma-separated string. For each Extent, the | 
 | 151 |   // string will have the start offset and then the length in bytes. | 
 | 152 |   // The length value of the last extent in the string may be short, since | 
 | 153 |   // the full length of all extents in the string is capped to full_length. | 
 | 154 |   // Also, an extent starting at kSparseHole, appears as -1 in the string. | 
 | 155 |   // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1}, | 
 | 156 |   // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13, | 
 | 157 |   // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083" | 
 | 158 |   static bool ExtentsToBsdiffPositionsString( | 
 | 159 |       const google::protobuf::RepeatedPtrField<Extent>& extents, | 
 | 160 |       uint64_t block_size, | 
 | 161 |       uint64_t full_length, | 
 | 162 |       std::string* positions_string); | 
 | 163 |  | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 164 |   // Returns true if a previous update attempt can be continued based on the | 
 | 165 |   // persistent preferences and the new update check response hash. | 
 | 166 |   static bool CanResumeUpdate(PrefsInterface* prefs, | 
 | 167 |                               std::string update_check_response_hash); | 
 | 168 |  | 
 | 169 |   // Resets the persistent update progress state to indicate that an update | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 170 |   // can't be resumed. Performs a quick update-in-progress reset if |quick| is | 
 | 171 |   // true, otherwise resets all progress-related update state. Returns true on | 
 | 172 |   // success, false otherwise. | 
 | 173 |   static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick); | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 174 |  | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 175 |   // Attempts to parse the update metadata starting from the beginning of | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 176 |   // |payload|. On success, returns kMetadataParseSuccess. Returns | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 177 |   // kMetadataParseInsufficientData if more data is needed to parse the complete | 
 | 178 |   // metadata. Returns kMetadataParseError if the metadata can't be parsed given | 
 | 179 |   // the payload. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 180 |   MetadataParseResult ParsePayloadMetadata(const chromeos::Blob& payload, | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 181 |                                            ErrorCode* error); | 
| Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 182 |  | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 183 |   void set_public_key_path(const std::string& public_key_path) { | 
 | 184 |     public_key_path_ = public_key_path; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 185 |   } | 
 | 186 |  | 
| Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 187 |   // Returns the byte offset at which the payload version can be found. | 
 | 188 |   static uint64_t GetVersionOffset(); | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 189 |  | 
 | 190 |   // Returns the byte offset where the size of the manifest is stored in | 
 | 191 |   // a payload. This offset precedes the actual start of the manifest | 
 | 192 |   // that's returned by the GetManifestOffset method. | 
 | 193 |   static uint64_t GetManifestSizeOffset(); | 
 | 194 |  | 
| Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 195 |   // Returns the byte offset at which the manifest protobuf begins in a | 
 | 196 |   // payload. | 
 | 197 |   static uint64_t GetManifestOffset(); | 
 | 198 |  | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 199 |   // Returns the size of the payload metadata, which includes the payload header | 
 | 200 |   // and the manifest. Is the header was not yet parsed, returns zero. | 
 | 201 |   uint64_t GetMetadataSize() const; | 
 | 202 |  | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 203 |   // If the manifest was successfully parsed, copies it to |*out_manifest_p|. | 
 | 204 |   // Returns true on success. | 
 | 205 |   bool GetManifest(DeltaArchiveManifest* out_manifest_p) const; | 
 | 206 |  | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 207 |   // Returns the delta minor version. If this value is defined in the manifest, | 
 | 208 |   // it returns that value, otherwise it returns the default value. | 
 | 209 |   uint32_t GetMinorVersion() const; | 
 | 210 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 211 |  private: | 
| Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 212 |   friend class DeltaPerformerTest; | 
 | 213 |   FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest); | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 214 |   FRIEND_TEST(DeltaPerformerTest, UsePublicKeyFromResponse); | 
| Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 215 |  | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 216 |   // Appends up to |*count_p| bytes from |*bytes_p| to |buffer_|, but only to | 
 | 217 |   // the extent that the size of |buffer_| does not exceed |max|. Advances | 
 | 218 |   // |*cbytes_p| and decreases |*count_p| by the actual number of bytes copied, | 
 | 219 |   // and returns this number. | 
 | 220 |   size_t CopyDataToBuffer(const char** bytes_p, size_t* count_p, size_t max); | 
 | 221 |  | 
 | 222 |   // If |op_result| is false, emits an error message using |op_type_name| and | 
 | 223 |   // sets |*error| accordingly. Otherwise does nothing. Returns |op_result|. | 
 | 224 |   bool HandleOpResult(bool op_result, const char* op_type_name, | 
 | 225 |                       ErrorCode* error); | 
 | 226 |  | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 227 |   // Logs the progress of downloading/applying an update. | 
 | 228 |   void LogProgress(const char* message_prefix); | 
 | 229 |  | 
 | 230 |   // Update overall progress metrics, log as necessary. | 
 | 231 |   void UpdateOverallProgress(bool force_log, const char* message_prefix); | 
 | 232 |  | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 233 |   // Verifies that the expected source partition hashes (if present) match the | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 234 |   // hashes for the current partitions. Returns true if there are no expected | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 235 |   // hashes in the payload (e.g., if it's a new-style full update) or if the | 
 | 236 |   // hashes match; returns false otherwise. | 
 | 237 |   bool VerifySourcePartitions(); | 
 | 238 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 239 |   // Returns true if enough of the delta file has been passed via Write() | 
 | 240 |   // to be able to perform a given install operation. | 
 | 241 |   bool CanPerformInstallOperation( | 
 | 242 |       const DeltaArchiveManifest_InstallOperation& operation); | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 243 |  | 
| Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 244 |   // Checks the integrity of the payload manifest. Returns true upon success, | 
 | 245 |   // false otherwise. | 
 | 246 |   ErrorCode ValidateManifest(); | 
 | 247 |  | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 248 |   // Validates that the hash of the blobs corresponding to the given |operation| | 
 | 249 |   // matches what's specified in the manifest in the payload. | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 250 |   // Returns ErrorCode::kSuccess on match or a suitable error code otherwise. | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 251 |   ErrorCode ValidateOperationHash( | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 252 |       const DeltaArchiveManifest_InstallOperation& operation); | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 253 |  | 
 | 254 |   // Interprets the given |protobuf| as a DeltaArchiveManifest protocol buffer | 
 | 255 |   // of the given protobuf_length and verifies that the signed hash of the | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 256 |   // metadata matches what's specified in the install plan from Omaha. | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 257 |   // Returns ErrorCode::kSuccess on match or a suitable error code otherwise. | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 258 |   // This method must be called before any part of the |protobuf| is parsed | 
 | 259 |   // so that a man-in-the-middle attack on the SSL connection to the payload | 
 | 260 |   // server doesn't exploit any vulnerability in the code that parses the | 
 | 261 |   // protocol buffer. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 262 |   ErrorCode ValidateMetadataSignature(const void* protobuf, | 
 | 263 |                                       uint64_t protobuf_length); | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 264 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 265 |   // Returns true on success. | 
 | 266 |   bool PerformInstallOperation( | 
 | 267 |       const DeltaArchiveManifest_InstallOperation& operation); | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 268 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 269 |   // These perform a specific type of operation and return true on success. | 
 | 270 |   bool PerformReplaceOperation( | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 271 |       const DeltaArchiveManifest_InstallOperation& operation, | 
 | 272 |       bool is_kernel_partition); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 273 |   bool PerformMoveOperation( | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 274 |       const DeltaArchiveManifest_InstallOperation& operation, | 
 | 275 |       bool is_kernel_partition); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 276 |   bool PerformBsdiffOperation( | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 277 |       const DeltaArchiveManifest_InstallOperation& operation, | 
 | 278 |       bool is_kernel_partition); | 
| Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 279 |   bool PerformSourceCopyOperation( | 
 | 280 |       const DeltaArchiveManifest_InstallOperation& operation, | 
 | 281 |       bool is_kernel_partition); | 
 | 282 |   bool PerformSourceBsdiffOperation( | 
 | 283 |       const DeltaArchiveManifest_InstallOperation& operation, | 
 | 284 |       bool is_kernel_partition); | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 285 |  | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 286 |   // Returns true if the payload signature message has been extracted from | 
 | 287 |   // |operation|, false otherwise. | 
 | 288 |   bool ExtractSignatureMessage( | 
 | 289 |       const DeltaArchiveManifest_InstallOperation& operation); | 
 | 290 |  | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 291 |   // Updates the hash calculator with the bytes in |buffer_|. Then discard the | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 292 |   // content, ensuring that memory is being deallocated. If |do_advance_offset|, | 
 | 293 |   // advances the internal offset counter accordingly. | 
 | 294 |   void DiscardBuffer(bool do_advance_offset); | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 295 |  | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 296 |   // Checkpoints the update progress into persistent storage to allow this | 
 | 297 |   // update attempt to be resumed after reboot. | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 298 |   bool CheckpointUpdateProgress(); | 
 | 299 |  | 
| Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 300 |   // Primes the required update state. Returns true if the update state was | 
 | 301 |   // successfully initialized to a saved resume state or if the update is a new | 
 | 302 |   // update. Returns false otherwise. | 
 | 303 |   bool PrimeUpdateState(); | 
 | 304 |  | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 305 |   // Sends UMA statistics for the given error code. | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 306 |   void SendUmaStat(ErrorCode code); | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 307 |  | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 308 |   // If the Omaha response contains a public RSA key and we're allowed | 
 | 309 |   // to use it (e.g. if we're in developer mode), extract the key from | 
 | 310 |   // the response and store it in a temporary file and return true. In | 
 | 311 |   // the affirmative the path to the temporary file is stored in | 
 | 312 |   // |out_tmp_key| and it is the responsibility of the caller to clean | 
 | 313 |   // it up. | 
 | 314 |   bool GetPublicKeyFromResponse(base::FilePath *out_tmp_key); | 
 | 315 |  | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 316 |   // Update Engine preference store. | 
 | 317 |   PrefsInterface* prefs_; | 
 | 318 |  | 
| Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 319 |   // Global context of the system. | 
 | 320 |   SystemState* system_state_; | 
 | 321 |  | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 322 |   // Install Plan based on Omaha Response. | 
 | 323 |   InstallPlan* install_plan_; | 
 | 324 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 325 |   // File descriptor of open device. | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 326 |   FileDescriptorPtr fd_; | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 327 |  | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 328 |   // File descriptor of the kernel device. | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 329 |   FileDescriptorPtr kernel_fd_; | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 330 |  | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 331 |   // File descriptor of the source device. | 
 | 332 |   FileDescriptorPtr source_fd_; | 
 | 333 |  | 
 | 334 |   // File descriptor of the source kernel device. | 
 | 335 |   FileDescriptorPtr source_kernel_fd_; | 
 | 336 |  | 
| Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 337 |   std::string path_;  // Path that fd_ refers to. | 
 | 338 |   std::string kernel_path_;  // Path that kernel_fd_ refers to. | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 339 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 340 |   DeltaArchiveManifest manifest_; | 
| Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 341 |   bool manifest_parsed_; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 342 |   bool manifest_valid_; | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 343 |   uint64_t metadata_size_; | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 344 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 345 |   // Index of the next operation to perform in the manifest. | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 346 |   size_t next_operation_num_; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 347 |  | 
| Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 348 |   // A buffer used for accumulating downloaded data. Initially, it stores the | 
 | 349 |   // payload metadata; once that's downloaded and parsed, it stores data for the | 
 | 350 |   // next update operation. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 351 |   chromeos::Blob buffer_; | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 352 |   // Offset of buffer_ in the binary blobs section of the update. | 
 | 353 |   uint64_t buffer_offset_; | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 354 |  | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 355 |   // Last |buffer_offset_| value updated as part of the progress update. | 
 | 356 |   uint64_t last_updated_buffer_offset_; | 
 | 357 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 358 |   // The block size (parsed from the manifest). | 
 | 359 |   uint32_t block_size_; | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 360 |  | 
| Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 361 |   // Calculates the payload hash. | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 362 |   OmahaHashCalculator hash_calculator_; | 
 | 363 |  | 
| Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 364 |   // Saves the signed hash context. | 
 | 365 |   std::string signed_hash_context_; | 
 | 366 |  | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 367 |   // Signatures message blob extracted directly from the payload. | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 368 |   chromeos::Blob signatures_message_data_; | 
| Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 369 |  | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 370 |   // The public key to be used. Provided as a member so that tests can | 
 | 371 |   // override with test keys. | 
 | 372 |   std::string public_key_path_; | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 373 |  | 
| Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 374 |   // The number of bytes received so far, used for progress tracking. | 
 | 375 |   size_t total_bytes_received_; | 
 | 376 |  | 
 | 377 |   // The number rootfs and total operations in a payload, once we know them. | 
 | 378 |   size_t num_rootfs_operations_; | 
 | 379 |   size_t num_total_operations_; | 
 | 380 |  | 
 | 381 |   // An overall progress counter, which should reflect both download progress | 
 | 382 |   // and the ratio of applied operations. Range is 0-100. | 
 | 383 |   unsigned overall_progress_; | 
 | 384 |  | 
 | 385 |   // The last progress chunk recorded. | 
 | 386 |   unsigned last_progress_chunk_; | 
 | 387 |  | 
 | 388 |   // The timeout after which we should force emitting a progress log (constant), | 
 | 389 |   // and the actual point in time for the next forced log to be emitted. | 
 | 390 |   const base::TimeDelta forced_progress_log_wait_; | 
 | 391 |   base::Time forced_progress_log_time_; | 
 | 392 |  | 
| Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 393 |   // The delta minor payload version supported by DeltaPerformer. | 
 | 394 |   uint32_t supported_minor_version_; | 
 | 395 |  | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 396 |   DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); | 
 | 397 | }; | 
 | 398 |  | 
 | 399 | }  // namespace chromeos_update_engine | 
 | 400 |  | 
| Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 401 | #endif  // UPDATE_ENGINE_DELTA_PERFORMER_H_ |