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 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_DELTA_PERFORMER_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_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 | |
Amin Hassani | db56be9 | 2017-09-06 12:41:23 -0700 | [diff] [blame] | 22 | #include <limits> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 23 | #include <string> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 24 | #include <vector> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 25 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 26 | #include <base/time/time.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 27 | #include <brillo/secure_blob.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 28 | #include <google/protobuf/repeated_field.h> |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 29 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 30 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/common/hash_calculator.h" |
| 32 | #include "update_engine/common/platform_constants.h" |
| 33 | #include "update_engine/payload_consumer/file_descriptor.h" |
| 34 | #include "update_engine/payload_consumer/file_writer.h" |
| 35 | #include "update_engine/payload_consumer/install_plan.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 | |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 40 | class DownloadActionDelegate; |
| 41 | class BootControlInterface; |
| 42 | class HardwareInterface; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 43 | class PrefsInterface; |
| 44 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 45 | // This class performs the actions in a delta update synchronously. The delta |
| 46 | // update itself should be passed in in chunks as it is received. |
| 47 | |
| 48 | class DeltaPerformer : public FileWriter { |
| 49 | public: |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 50 | enum MetadataParseResult { |
| 51 | kMetadataParseSuccess, |
| 52 | kMetadataParseError, |
| 53 | kMetadataParseInsufficientData, |
| 54 | }; |
| 55 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 56 | static const uint64_t kDeltaVersionOffset; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 57 | static const uint64_t kDeltaVersionSize; |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 58 | static const uint64_t kDeltaManifestSizeOffset; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 59 | static const uint64_t kDeltaManifestSizeSize; |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 60 | static const uint64_t kDeltaMetadataSignatureSizeSize; |
| 61 | static const uint64_t kMaxPayloadHeaderSize; |
Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 62 | static const uint64_t kSupportedMajorPayloadVersion; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 63 | static const uint32_t kSupportedMinorPayloadVersion; |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 64 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 65 | // Defines the granularity of progress logging in terms of how many "completed |
| 66 | // chunks" we want to report at the most. |
| 67 | static const unsigned kProgressLogMaxChunks; |
| 68 | // Defines a timeout since the last progress was logged after which we want to |
| 69 | // force another log message (even if the current chunk was not completed). |
| 70 | static const unsigned kProgressLogTimeoutSeconds; |
| 71 | // These define the relative weights (0-100) we give to the different work |
| 72 | // components associated with an update when computing an overall progress. |
| 73 | // Currently they include the download progress and the number of completed |
| 74 | // operations. They must add up to one hundred (100). |
| 75 | static const unsigned kProgressDownloadWeight; |
| 76 | static const unsigned kProgressOperationsWeight; |
| 77 | |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 78 | DeltaPerformer(PrefsInterface* prefs, |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 79 | BootControlInterface* boot_control, |
| 80 | HardwareInterface* hardware, |
| 81 | DownloadActionDelegate* download_delegate, |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 82 | InstallPlan* install_plan, |
Sen Jiang | 1841408 | 2018-01-11 14:50:36 -0800 | [diff] [blame] | 83 | InstallPlan::Payload* payload, |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 84 | bool interactive) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 85 | : prefs_(prefs), |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 86 | boot_control_(boot_control), |
| 87 | hardware_(hardware), |
| 88 | download_delegate_(download_delegate), |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 89 | install_plan_(install_plan), |
Sen Jiang | 1841408 | 2018-01-11 14:50:36 -0800 | [diff] [blame] | 90 | payload_(payload), |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 91 | interactive_(interactive) {} |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 92 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 93 | // FileWriter's Write implementation where caller doesn't care about |
| 94 | // error codes. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 95 | bool Write(const void* bytes, size_t count) override { |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 96 | ErrorCode error; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 97 | return Write(bytes, count, &error); |
| 98 | } |
| 99 | |
| 100 | // FileWriter's Write implementation that returns a more specific |error| code |
| 101 | // in case of failures in Write operation. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 102 | 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] | 103 | |
| 104 | // 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] | 105 | // Closes both 'path' given to Open() and the kernel path. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 106 | int Close() override; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 107 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 108 | // Open the target and source (if delta payload) file descriptors for the |
| 109 | // |current_partition_|. The manifest needs to be already parsed for this to |
| 110 | // work. Returns whether the required file descriptors were successfully open. |
| 111 | bool OpenCurrentPartition(); |
| 112 | |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame^] | 113 | // Attempt to open the error-corrected device for the current partition. |
| 114 | // Returns whether the operation succeeded. |
| 115 | bool OpenCurrentECCPartition(); |
| 116 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 117 | // Closes the current partition file descriptors if open. Returns 0 on success |
| 118 | // or -errno on error. |
| 119 | int CloseCurrentPartition(); |
| 120 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 121 | // Returns |true| only if the manifest has been processed and it's valid. |
| 122 | bool IsManifestValid(); |
| 123 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 124 | // Verifies the downloaded payload against the signed hash included in the |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 125 | // payload, against the update check hash and size using the public key and |
| 126 | // returns ErrorCode::kSuccess on success, an error code on failure. |
| 127 | // This method should be called after closing the stream. Note this method |
| 128 | // skips the signed hash check if the public key is unavailable; it returns |
| 129 | // ErrorCode::kSignedDeltaPayloadExpectedError if the public key is available |
| 130 | // but the delta payload doesn't include a signature. |
| 131 | ErrorCode VerifyPayload(const brillo::Blob& update_check_response_hash, |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 132 | const uint64_t update_check_response_size); |
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 | // Converts an ordered collection of Extent objects which contain data of |
| 135 | // length full_length to a comma-separated string. For each Extent, the |
| 136 | // string will have the start offset and then the length in bytes. |
| 137 | // The length value of the last extent in the string may be short, since |
| 138 | // the full length of all extents in the string is capped to full_length. |
| 139 | // Also, an extent starting at kSparseHole, appears as -1 in the string. |
| 140 | // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1}, |
| 141 | // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13, |
| 142 | // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083" |
| 143 | static bool ExtentsToBsdiffPositionsString( |
| 144 | const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 145 | uint64_t block_size, |
| 146 | uint64_t full_length, |
| 147 | std::string* positions_string); |
| 148 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 149 | // Returns true if a previous update attempt can be continued based on the |
| 150 | // persistent preferences and the new update check response hash. |
| 151 | static bool CanResumeUpdate(PrefsInterface* prefs, |
Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 152 | const std::string& update_check_response_hash); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 153 | |
| 154 | // Resets the persistent update progress state to indicate that an update |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 155 | // can't be resumed. Performs a quick update-in-progress reset if |quick| is |
| 156 | // true, otherwise resets all progress-related update state. Returns true on |
| 157 | // success, false otherwise. |
| 158 | static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 159 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 160 | // Attempts to parse the update metadata starting from the beginning of |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 161 | // |payload|. On success, returns kMetadataParseSuccess. Returns |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 162 | // kMetadataParseInsufficientData if more data is needed to parse the complete |
| 163 | // metadata. Returns kMetadataParseError if the metadata can't be parsed given |
| 164 | // the payload. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 165 | MetadataParseResult ParsePayloadMetadata(const brillo::Blob& payload, |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 166 | ErrorCode* error); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 167 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 168 | void set_public_key_path(const std::string& public_key_path) { |
| 169 | public_key_path_ = public_key_path; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Amin Hassani | db56be9 | 2017-09-06 12:41:23 -0700 | [diff] [blame] | 172 | // Set |*out_offset| to the byte offset where the size of the metadata |
| 173 | // signature is stored in a payload. Return true on success, if this field is |
| 174 | // not present in the payload, return false. |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 175 | bool GetMetadataSignatureSizeOffset(uint64_t* out_offset) const; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 176 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 177 | // Set |*out_offset| to the byte offset at which the manifest protobuf begins |
| 178 | // in a payload. Return true on success, false if the offset is unknown. |
| 179 | bool GetManifestOffset(uint64_t* out_offset) const; |
Don Garrett | 4d03944 | 2013-10-28 18:40:06 -0700 | [diff] [blame] | 180 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 181 | // Returns the size of the payload metadata, which includes the payload header |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 182 | // and the manifest. If the header was not yet parsed, returns zero. |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 183 | uint64_t GetMetadataSize() const; |
| 184 | |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 185 | // If the manifest was successfully parsed, copies it to |*out_manifest_p|. |
| 186 | // Returns true on success. |
| 187 | bool GetManifest(DeltaArchiveManifest* out_manifest_p) const; |
| 188 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 189 | // Return true if header parsing is finished and no errors occurred. |
| 190 | bool IsHeaderParsed() const; |
| 191 | |
| 192 | // Returns the major payload version. If the version was not yet parsed, |
| 193 | // returns zero. |
| 194 | uint64_t GetMajorVersion() const; |
| 195 | |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 196 | // Returns the delta minor version. If this value is defined in the manifest, |
| 197 | // it returns that value, otherwise it returns the default value. |
| 198 | uint32_t GetMinorVersion() const; |
| 199 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 200 | private: |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 201 | friend class DeltaPerformerTest; |
Sen Jiang | a4365d6 | 2015-09-25 10:52:25 -0700 | [diff] [blame] | 202 | friend class DeltaPerformerIntegrationTest; |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame] | 203 | FRIEND_TEST(DeltaPerformerTest, BrilloMetadataSignatureSizeTest); |
| 204 | FRIEND_TEST(DeltaPerformerTest, BrilloVerifyMetadataSignatureTest); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 205 | FRIEND_TEST(DeltaPerformerTest, UsePublicKeyFromResponse); |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 206 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 207 | // Parse and move the update instructions of all partitions into our local |
| 208 | // |partitions_| variable based on the version of the payload. Requires the |
| 209 | // manifest to be parsed and valid. |
| 210 | bool ParseManifestPartitions(ErrorCode* error); |
| 211 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 212 | // Appends up to |*count_p| bytes from |*bytes_p| to |buffer_|, but only to |
| 213 | // the extent that the size of |buffer_| does not exceed |max|. Advances |
| 214 | // |*cbytes_p| and decreases |*count_p| by the actual number of bytes copied, |
| 215 | // and returns this number. |
| 216 | size_t CopyDataToBuffer(const char** bytes_p, size_t* count_p, size_t max); |
| 217 | |
| 218 | // If |op_result| is false, emits an error message using |op_type_name| and |
| 219 | // sets |*error| accordingly. Otherwise does nothing. Returns |op_result|. |
| 220 | bool HandleOpResult(bool op_result, const char* op_type_name, |
| 221 | ErrorCode* error); |
| 222 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 223 | // Logs the progress of downloading/applying an update. |
| 224 | void LogProgress(const char* message_prefix); |
| 225 | |
| 226 | // Update overall progress metrics, log as necessary. |
| 227 | void UpdateOverallProgress(bool force_log, const char* message_prefix); |
| 228 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 229 | // Returns true if enough of the delta file has been passed via Write() |
| 230 | // to be able to perform a given install operation. |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 231 | bool CanPerformInstallOperation(const InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 232 | |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 233 | // Checks the integrity of the payload manifest. Returns true upon success, |
| 234 | // false otherwise. |
| 235 | ErrorCode ValidateManifest(); |
| 236 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 237 | // Validates that the hash of the blobs corresponding to the given |operation| |
| 238 | // matches what's specified in the manifest in the payload. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 239 | // Returns ErrorCode::kSuccess on match or a suitable error code otherwise. |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 240 | ErrorCode ValidateOperationHash(const InstallOperation& operation); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 241 | |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame] | 242 | // Given the |payload|, verifies that the signed hash of its metadata matches |
| 243 | // what's specified in the install plan from Omaha (if present) or the |
| 244 | // metadata signature in payload itself (if present). Returns |
| 245 | // ErrorCode::kSuccess on match or a suitable error code otherwise. This |
| 246 | // method must be called before any part of the metadata is parsed so that a |
| 247 | // man-in-the-middle attack on the SSL connection to the payload server |
| 248 | // doesn't exploit any vulnerability in the code that parses the protocol |
| 249 | // buffer. |
| 250 | ErrorCode ValidateMetadataSignature(const brillo::Blob& payload); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 251 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 252 | // Returns true on success. |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 253 | bool PerformInstallOperation(const InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 254 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 255 | // These perform a specific type of operation and return true on success. |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 256 | // |error| will be set if source hash mismatch, otherwise |error| might not be |
| 257 | // set even if it fails. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 258 | bool PerformReplaceOperation(const InstallOperation& operation); |
| 259 | bool PerformZeroOrDiscardOperation(const InstallOperation& operation); |
| 260 | bool PerformMoveOperation(const InstallOperation& operation); |
| 261 | bool PerformBsdiffOperation(const InstallOperation& operation); |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 262 | bool PerformSourceCopyOperation(const InstallOperation& operation, |
| 263 | ErrorCode* error); |
| 264 | bool PerformSourceBsdiffOperation(const InstallOperation& operation, |
| 265 | ErrorCode* error); |
Amin Hassani | 02855c2 | 2017-09-06 22:34:50 -0700 | [diff] [blame] | 266 | bool PerformPuffDiffOperation(const InstallOperation& operation, |
| 267 | ErrorCode* error); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 268 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 269 | // Extracts the payload signature message from the blob on the |operation| if |
| 270 | // the offset matches the one specified by the manifest. Returns whether the |
| 271 | // signature was extracted. |
| 272 | bool ExtractSignatureMessageFromOperation(const InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 273 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 274 | // Extracts the payload signature message from the current |buffer_| if the |
| 275 | // offset matches the one specified by the manifest. Returns whether the |
| 276 | // signature was extracted. |
| 277 | bool ExtractSignatureMessage(); |
| 278 | |
| 279 | // Updates the payload hash calculator with the bytes in |buffer_|, also |
| 280 | // updates the signed hash calculator with the first |signed_hash_buffer_size| |
| 281 | // bytes in |buffer_|. Then discard the content, ensuring that memory is being |
| 282 | // deallocated. If |do_advance_offset|, advances the internal offset counter |
| 283 | // accordingly. |
| 284 | void DiscardBuffer(bool do_advance_offset, size_t signed_hash_buffer_size); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 285 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 286 | // Checkpoints the update progress into persistent storage to allow this |
| 287 | // update attempt to be resumed after reboot. |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 288 | bool CheckpointUpdateProgress(); |
| 289 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 290 | // Primes the required update state. Returns true if the update state was |
| 291 | // successfully initialized to a saved resume state or if the update is a new |
| 292 | // update. Returns false otherwise. |
| 293 | bool PrimeUpdateState(); |
| 294 | |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 295 | // If the Omaha response contains a public RSA key and we're allowed |
| 296 | // to use it (e.g. if we're in developer mode), extract the key from |
| 297 | // the response and store it in a temporary file and return true. In |
| 298 | // the affirmative the path to the temporary file is stored in |
| 299 | // |out_tmp_key| and it is the responsibility of the caller to clean |
| 300 | // it up. |
| 301 | bool GetPublicKeyFromResponse(base::FilePath *out_tmp_key); |
| 302 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 303 | // Update Engine preference store. |
| 304 | PrefsInterface* prefs_; |
| 305 | |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 306 | // BootControl and Hardware interface references. |
| 307 | BootControlInterface* boot_control_; |
| 308 | HardwareInterface* hardware_; |
| 309 | |
| 310 | // The DownloadActionDelegate instance monitoring the DownloadAction, or a |
| 311 | // nullptr if not used. |
| 312 | DownloadActionDelegate* download_delegate_; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 313 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 314 | // Install Plan based on Omaha Response. |
| 315 | InstallPlan* install_plan_; |
| 316 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 317 | // Pointer to the current payload in install_plan_.payloads. |
| 318 | InstallPlan::Payload* payload_{nullptr}; |
| 319 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 320 | // File descriptor of the source partition. Only set while updating a |
| 321 | // partition when using a delta payload. |
| 322 | FileDescriptorPtr source_fd_{nullptr}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 323 | |
Alex Deymo | 51c264e | 2016-11-04 15:49:53 -0700 | [diff] [blame^] | 324 | // File descriptor of the error corrected source partition. Only set while |
| 325 | // updating partition using a delta payload for a partition where error |
| 326 | // correction is available. The size of the error corrected device is smaller |
| 327 | // than the underlying raw device, since it doesn't include the error |
| 328 | // correction blocks. |
| 329 | FileDescriptorPtr source_ecc_fd_{nullptr}; |
| 330 | |
| 331 | // The total number of operations that failed source hash verification but |
| 332 | // passed after falling back to the error-corrected |source_ecc_fd_| device. |
| 333 | uint64_t source_ecc_recovered_failures_{0}; |
| 334 | |
| 335 | // Whether opening the current partition as an error-corrected device failed. |
| 336 | // Used to avoid re-opening the same source partition if it is not actually |
| 337 | // error corrected. |
| 338 | bool source_ecc_open_failure_{false}; |
| 339 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 340 | // File descriptor of the target partition. Only set while performing the |
| 341 | // operations of a given partition. |
| 342 | FileDescriptorPtr target_fd_{nullptr}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 343 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 344 | // Paths the |source_fd_| and |target_fd_| refer to. |
| 345 | std::string source_path_; |
| 346 | std::string target_path_; |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 347 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 348 | // Parsed manifest. Set after enough bytes to parse the manifest were |
| 349 | // downloaded. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 350 | DeltaArchiveManifest manifest_; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 351 | bool manifest_parsed_{false}; |
| 352 | bool manifest_valid_{false}; |
| 353 | uint64_t metadata_size_{0}; |
| 354 | uint64_t manifest_size_{0}; |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame] | 355 | uint32_t metadata_signature_size_{0}; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 356 | uint64_t major_payload_version_{0}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 357 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 358 | // Accumulated number of operations per partition. The i-th element is the |
| 359 | // sum of the number of operations for all the partitions from 0 to i |
| 360 | // inclusive. Valid when |manifest_valid_| is true. |
| 361 | std::vector<size_t> acc_num_operations_; |
| 362 | |
| 363 | // The total operations in a payload. Valid when |manifest_valid_| is true, |
| 364 | // otherwise 0. |
| 365 | size_t num_total_operations_{0}; |
| 366 | |
| 367 | // The list of partitions to update as found in the manifest major version 2. |
| 368 | // When parsing an older manifest format, the information is converted over to |
| 369 | // this format instead. |
| 370 | std::vector<PartitionUpdate> partitions_; |
| 371 | |
| 372 | // Index in the list of partitions (|partitions_| member) of the current |
| 373 | // partition being processed. |
| 374 | size_t current_partition_{0}; |
| 375 | |
| 376 | // Index of the next operation to perform in the manifest. The index is linear |
| 377 | // on the total number of operation on the manifest. |
| 378 | size_t next_operation_num_{0}; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 379 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 380 | // A buffer used for accumulating downloaded data. Initially, it stores the |
| 381 | // payload metadata; once that's downloaded and parsed, it stores data for the |
| 382 | // next update operation. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 383 | brillo::Blob buffer_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 384 | // Offset of buffer_ in the binary blobs section of the update. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 385 | uint64_t buffer_offset_{0}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 386 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 387 | // Last |buffer_offset_| value updated as part of the progress update. |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 388 | uint64_t last_updated_buffer_offset_{std::numeric_limits<uint64_t>::max()}; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 389 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 390 | // The block size (parsed from the manifest). |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 391 | uint32_t block_size_{0}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 392 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 393 | // Calculates the whole payload file hash, including headers and signatures. |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 394 | HashCalculator payload_hash_calculator_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 395 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 396 | // Calculates the hash of the portion of the payload signed by the payload |
| 397 | // signature. This hash skips the metadata signature portion, located after |
| 398 | // the metadata and doesn't include the payload signature itself. |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 399 | HashCalculator signed_hash_calculator_; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 400 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 401 | // Signatures message blob extracted directly from the payload. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 402 | brillo::Blob signatures_message_data_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 403 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 404 | // The public key to be used. Provided as a member so that tests can |
| 405 | // override with test keys. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 406 | std::string public_key_path_{constants::kUpdatePayloadPublicKeyPath}; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 407 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 408 | // The number of bytes received so far, used for progress tracking. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 409 | size_t total_bytes_received_{0}; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 410 | |
| 411 | // An overall progress counter, which should reflect both download progress |
| 412 | // and the ratio of applied operations. Range is 0-100. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 413 | unsigned overall_progress_{0}; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 414 | |
| 415 | // The last progress chunk recorded. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 416 | unsigned last_progress_chunk_{0}; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 417 | |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 418 | // If |true|, the update is user initiated (vs. periodic update checks). |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 419 | bool interactive_{false}; |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 420 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 421 | // The timeout after which we should force emitting a progress log (constant), |
| 422 | // and the actual point in time for the next forced log to be emitted. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 423 | const base::TimeDelta forced_progress_log_wait_{ |
| 424 | base::TimeDelta::FromSeconds(kProgressLogTimeoutSeconds)}; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 425 | base::Time forced_progress_log_time_; |
| 426 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 427 | // The payload major payload version supported by DeltaPerformer. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 428 | uint64_t supported_major_version_{kSupportedMajorPayloadVersion}; |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 429 | |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 430 | // The delta minor payload version supported by DeltaPerformer. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 431 | uint32_t supported_minor_version_{kSupportedMinorPayloadVersion}; |
Allie Wood | fdf0051 | 2015-03-02 13:34:55 -0800 | [diff] [blame] | 432 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 433 | DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); |
| 434 | }; |
| 435 | |
| 436 | } // namespace chromeos_update_engine |
| 437 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 438 | #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_DELTA_PERFORMER_H_ |