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