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> |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 23 | #include <memory> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 24 | #include <string> |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 25 | #include <utility> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 26 | #include <vector> |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 27 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 28 | #include <base/time/time.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 29 | #include <brillo/secure_blob.h> |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 30 | #include <google/protobuf/repeated_field.h> |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 31 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 32 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/hash_calculator.h" |
| 34 | #include "update_engine/common/platform_constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 35 | #include "update_engine/payload_consumer/file_writer.h" |
| 36 | #include "update_engine/payload_consumer/install_plan.h" |
Kelvin Zhang | cfc531f | 2022-08-24 17:58:53 +0000 | [diff] [blame] | 37 | #include "update_engine/payload_consumer/partition_writer_interface.h" |
Sen Jiang | b5f601d | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 38 | #include "update_engine/payload_consumer/payload_metadata.h" |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 39 | #include "update_engine/payload_consumer/payload_verifier.h" |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | #include "update_engine/update_metadata.pb.h" |
| 41 | |
| 42 | namespace chromeos_update_engine { |
| 43 | |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 44 | class DownloadActionDelegate; |
| 45 | class BootControlInterface; |
| 46 | class HardwareInterface; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 47 | class PrefsInterface; |
| 48 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 49 | // This class performs the actions in a delta update synchronously. The delta |
| 50 | // update itself should be passed in in chunks as it is received. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 51 | class DeltaPerformer : public FileWriter { |
| 52 | public: |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 53 | // Defines the granularity of progress logging in terms of how many "completed |
| 54 | // chunks" we want to report at the most. |
| 55 | static const unsigned kProgressLogMaxChunks; |
| 56 | // Defines a timeout since the last progress was logged after which we want to |
| 57 | // force another log message (even if the current chunk was not completed). |
| 58 | static const unsigned kProgressLogTimeoutSeconds; |
| 59 | // These define the relative weights (0-100) we give to the different work |
| 60 | // components associated with an update when computing an overall progress. |
| 61 | // Currently they include the download progress and the number of completed |
| 62 | // operations. They must add up to one hundred (100). |
| 63 | static const unsigned kProgressDownloadWeight; |
| 64 | static const unsigned kProgressOperationsWeight; |
Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 65 | static const uint64_t kCheckpointFrequencySeconds; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 66 | |
Kelvin Zhang | 1304fe7 | 2021-10-06 19:12:12 -0700 | [diff] [blame] | 67 | DeltaPerformer( |
| 68 | PrefsInterface* prefs, |
| 69 | BootControlInterface* boot_control, |
| 70 | HardwareInterface* hardware, |
| 71 | DownloadActionDelegate* download_delegate, |
| 72 | InstallPlan* install_plan, |
| 73 | InstallPlan::Payload* payload, |
| 74 | bool interactive, |
| 75 | std::string update_certificates_path = constants::kUpdateCertificatesPath) |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 76 | : prefs_(prefs), |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 77 | boot_control_(boot_control), |
| 78 | hardware_(hardware), |
| 79 | download_delegate_(download_delegate), |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 80 | install_plan_(install_plan), |
Sen Jiang | 1841408 | 2018-01-11 14:50:36 -0800 | [diff] [blame] | 81 | payload_(payload), |
Kelvin Zhang | 1304fe7 | 2021-10-06 19:12:12 -0700 | [diff] [blame] | 82 | update_certificates_path_(std::move(update_certificates_path)), |
Kelvin Zhang | cc011d3 | 2020-07-10 18:20:08 -0400 | [diff] [blame] | 83 | interactive_(interactive) { |
| 84 | CHECK(install_plan_); |
| 85 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 86 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 87 | // FileWriter's Write implementation where caller doesn't care about |
| 88 | // error codes. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 89 | bool Write(const void* bytes, size_t count) override { |
Kelvin Zhang | cfc531f | 2022-08-24 17:58:53 +0000 | [diff] [blame] | 90 | ErrorCode error{}; |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 91 | return Write(bytes, count, &error); |
| 92 | } |
| 93 | |
| 94 | // FileWriter's Write implementation that returns a more specific |error| code |
| 95 | // in case of failures in Write operation. |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 96 | 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] | 97 | |
| 98 | // 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] | 99 | // Closes both 'path' given to Open() and the kernel path. |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 100 | int Close() override; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 101 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 102 | // Open the target and source (if delta payload) file descriptors for the |
| 103 | // |current_partition_|. The manifest needs to be already parsed for this to |
| 104 | // work. Returns whether the required file descriptors were successfully open. |
| 105 | bool OpenCurrentPartition(); |
| 106 | |
| 107 | // Closes the current partition file descriptors if open. Returns 0 on success |
| 108 | // or -errno on error. |
| 109 | int CloseCurrentPartition(); |
| 110 | |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 111 | // Returns |true| only if the manifest has been processed and it's valid. |
| 112 | bool IsManifestValid(); |
| 113 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 114 | // Verifies the downloaded payload against the signed hash included in the |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 115 | // payload, against the update check hash and size using the public key and |
| 116 | // returns ErrorCode::kSuccess on success, an error code on failure. |
| 117 | // This method should be called after closing the stream. Note this method |
| 118 | // skips the signed hash check if the public key is unavailable; it returns |
| 119 | // ErrorCode::kSignedDeltaPayloadExpectedError if the public key is available |
| 120 | // but the delta payload doesn't include a signature. |
| 121 | ErrorCode VerifyPayload(const brillo::Blob& update_check_response_hash, |
Allie Wood | 9f6f0a5 | 2015-03-30 11:25:47 -0700 | [diff] [blame] | 122 | const uint64_t update_check_response_size); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 123 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 124 | // Converts an ordered collection of Extent objects which contain data of |
| 125 | // length full_length to a comma-separated string. For each Extent, the |
| 126 | // string will have the start offset and then the length in bytes. |
| 127 | // The length value of the last extent in the string may be short, since |
| 128 | // the full length of all extents in the string is capped to full_length. |
| 129 | // Also, an extent starting at kSparseHole, appears as -1 in the string. |
| 130 | // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1}, |
| 131 | // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13, |
| 132 | // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083" |
| 133 | static bool ExtentsToBsdiffPositionsString( |
| 134 | const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 135 | uint64_t block_size, |
| 136 | uint64_t full_length, |
| 137 | std::string* positions_string); |
| 138 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 139 | // Returns true if a previous update attempt can be continued based on the |
| 140 | // persistent preferences and the new update check response hash. |
| 141 | static bool CanResumeUpdate(PrefsInterface* prefs, |
Chih-Hung Hsieh | 5c6bb1d | 2016-07-27 13:33:15 -0700 | [diff] [blame] | 142 | const std::string& update_check_response_hash); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 143 | |
| 144 | // Resets the persistent update progress state to indicate that an update |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 145 | // can't be resumed. Performs a quick update-in-progress reset if |quick| is |
Yifan Hong | 55c2bfe | 2020-01-13 17:01:19 -0800 | [diff] [blame] | 146 | // true, otherwise resets all progress-related update state. |
| 147 | // If |skip_dynamic_partititon_metadata_updated| is true, do not reset |
| 148 | // dynamic-partition-metadata-updated. |
| 149 | // Returns true on success, false otherwise. |
Daniel Zheng | f978480 | 2023-06-08 08:39:48 -0700 | [diff] [blame] | 150 | static bool ResetUpdateProgress( |
| 151 | PrefsInterface* prefs, |
| 152 | bool quick, |
| 153 | bool skip_dynamic_partititon_metadata_updated = false); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 154 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 155 | // Attempts to parse the update metadata starting from the beginning of |
Gilad Arnold | daa2740 | 2014-01-23 11:56:17 -0800 | [diff] [blame] | 156 | // |payload|. On success, returns kMetadataParseSuccess. Returns |
Daniel Zheng | 5eece04 | 2023-05-17 14:44:10 -0700 | [diff] [blame] | 157 | // kMetadataParseInsufficientData if more data is needed to parse the |
| 158 | // complete metadata. Returns kMetadataParseError if the metadata can't be |
| 159 | // parsed given the payload. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 160 | MetadataParseResult ParsePayloadMetadata(const brillo::Blob& payload, |
Daniel Zheng | eede4c8 | 2023-06-13 11:21:06 -0700 | [diff] [blame] | 161 | ErrorCode* error = nullptr); |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 162 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 163 | void set_public_key_path(const std::string& public_key_path) { |
| 164 | public_key_path_ = public_key_path; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Sen Jiang | b8060e4 | 2015-09-24 17:30:50 -0700 | [diff] [blame] | 167 | // Return true if header parsing is finished and no errors occurred. |
| 168 | bool IsHeaderParsed() const; |
| 169 | |
Kelvin Zhang | e4235b0 | 2020-11-23 13:57:51 -0500 | [diff] [blame] | 170 | // Checkpoints the update progress into persistent storage to allow this |
| 171 | // update attempt to be resumed after reboot. |
| 172 | // If |force| is false, checkpoint may be throttled. |
| 173 | // Exposed for testing purposes. |
| 174 | bool CheckpointUpdateProgress(bool force); |
| 175 | |
Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 176 | // Initialize partitions and allocate required space for an update with the |
| 177 | // given |manifest|. |update_check_response_hash| is used to check if the |
| 178 | // previous call to this function corresponds to the same payload. |
| 179 | // - Same payload: not make any persistent modifications (not write to disk) |
| 180 | // - Different payload: make persistent modifications (write to disk) |
| 181 | // In both cases, in-memory flags are updated. This function must be called |
| 182 | // on the payload at least once (to update in-memory flags) before writing |
| 183 | // (applying) the payload. |
| 184 | // If error due to insufficient space, |required_size| is set to the required |
| 185 | // size on the device to apply the payload. |
| 186 | static bool PreparePartitionsForUpdate( |
| 187 | PrefsInterface* prefs, |
| 188 | BootControlInterface* boot_control, |
| 189 | BootControlInterface::Slot target_slot, |
| 190 | const DeltaArchiveManifest& manifest, |
| 191 | const std::string& update_check_response_hash, |
Daniel Zheng | eede4c8 | 2023-06-13 11:21:06 -0700 | [diff] [blame] | 192 | uint64_t* required_size, |
| 193 | ErrorCode* error = nullptr); |
Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 194 | |
Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 195 | protected: |
| 196 | // Exposed as virtual for testing purposes. |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 197 | virtual std::unique_ptr<PartitionWriterInterface> CreatePartitionWriter( |
Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 198 | const PartitionUpdate& partition_update, |
| 199 | const InstallPlan::Partition& install_part, |
| 200 | DynamicPartitionControlInterface* dynamic_control, |
| 201 | size_t block_size, |
| 202 | bool is_interactive, |
| 203 | bool is_dynamic_partition); |
| 204 | |
| 205 | // return true if it has been long enough and a checkpoint should be saved. |
| 206 | // Exposed for unittest purposes. |
| 207 | virtual bool ShouldCheckpoint(); |
| 208 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 209 | private: |
Andrew de los Reyes | 353777c | 2010-10-08 10:34:30 -0700 | [diff] [blame] | 210 | friend class DeltaPerformerTest; |
Sen Jiang | a4365d6 | 2015-09-25 10:52:25 -0700 | [diff] [blame] | 211 | friend class DeltaPerformerIntegrationTest; |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame] | 212 | FRIEND_TEST(DeltaPerformerTest, BrilloMetadataSignatureSizeTest); |
Sen Jiang | b5f601d | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 213 | FRIEND_TEST(DeltaPerformerTest, BrilloParsePayloadMetadataTest); |
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 | |
Kelvin Zhang | 52cb1d7 | 2020-10-27 13:44:25 -0400 | [diff] [blame] | 216 | // Obtain the operation index for current partition. If all operations for |
| 217 | // current partition is are finished, return # of operations. This is mostly |
| 218 | // intended to be used by CheckpointUpdateProgress, where partition writer |
| 219 | // needs to know the current operation number to properly checkpoint update. |
| 220 | size_t GetPartitionOperationNum(); |
| 221 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 222 | // Parse and move the update instructions of all partitions into our local |
| 223 | // |partitions_| variable based on the version of the payload. Requires the |
| 224 | // manifest to be parsed and valid. |
Daniel Zheng | eede4c8 | 2023-06-13 11:21:06 -0700 | [diff] [blame] | 225 | bool ParseManifestPartitions(ErrorCode* error = nullptr); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 226 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 227 | // Appends up to |*count_p| bytes from |*bytes_p| to |buffer_|, but only to |
| 228 | // the extent that the size of |buffer_| does not exceed |max|. Advances |
| 229 | // |*cbytes_p| and decreases |*count_p| by the actual number of bytes copied, |
| 230 | // and returns this number. |
| 231 | size_t CopyDataToBuffer(const char** bytes_p, size_t* count_p, size_t max); |
| 232 | |
| 233 | // If |op_result| is false, emits an error message using |op_type_name| and |
| 234 | // sets |*error| accordingly. Otherwise does nothing. Returns |op_result|. |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 235 | bool HandleOpResult(bool op_result, |
| 236 | const char* op_type_name, |
Daniel Zheng | eede4c8 | 2023-06-13 11:21:06 -0700 | [diff] [blame] | 237 | ErrorCode* error = nullptr); |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 238 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 239 | // Logs the progress of downloading/applying an update. |
| 240 | void LogProgress(const char* message_prefix); |
| 241 | |
| 242 | // Update overall progress metrics, log as necessary. |
| 243 | void UpdateOverallProgress(bool force_log, const char* message_prefix); |
| 244 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 245 | // Returns true if enough of the delta file has been passed via Write() |
| 246 | // to be able to perform a given install operation. |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 247 | bool CanPerformInstallOperation(const InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 248 | |
Daniel Zheng | 0dc25a6 | 2024-01-29 13:01:53 -0800 | [diff] [blame^] | 249 | // Process one InstallOperation |
| 250 | bool ProcessOperation(const InstallOperation* op, ErrorCode* error); |
Gilad Arnold | 21504f0 | 2013-05-24 08:51:22 -0700 | [diff] [blame] | 251 | // Checks the integrity of the payload manifest. Returns true upon success, |
| 252 | // false otherwise. |
| 253 | ErrorCode ValidateManifest(); |
| 254 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 255 | // Validates that the hash of the blobs corresponding to the given |operation| |
| 256 | // matches what's specified in the manifest in the payload. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 257 | // Returns ErrorCode::kSuccess on match or a suitable error code otherwise. |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 258 | ErrorCode ValidateOperationHash(const InstallOperation& operation); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 259 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 260 | // Returns true on success. |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 261 | bool PerformInstallOperation(const InstallOperation& operation); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 262 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 263 | // These perform a specific type of operation and return true on success. |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 264 | // |error| will be set if source hash mismatch, otherwise |error| might not be |
| 265 | // set even if it fails. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 266 | bool PerformReplaceOperation(const InstallOperation& operation); |
| 267 | bool PerformZeroOrDiscardOperation(const InstallOperation& operation); |
Sen Jiang | be2c47b | 2016-06-15 14:09:27 -0700 | [diff] [blame] | 268 | bool PerformSourceCopyOperation(const InstallOperation& operation, |
Daniel Zheng | eede4c8 | 2023-06-13 11:21:06 -0700 | [diff] [blame] | 269 | ErrorCode* error = nullptr); |
Tianjie | 8e0090d | 2021-08-30 22:35:21 -0700 | [diff] [blame] | 270 | bool PerformDiffOperation(const InstallOperation& operation, |
Daniel Zheng | eede4c8 | 2023-06-13 11:21:06 -0700 | [diff] [blame] | 271 | ErrorCode* error = nullptr); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 272 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 273 | // Extracts the payload signature message from the current |buffer_| if the |
| 274 | // offset matches the one specified by the manifest. Returns whether the |
| 275 | // signature was extracted. |
| 276 | bool ExtractSignatureMessage(); |
| 277 | |
| 278 | // Updates the payload hash calculator with the bytes in |buffer_|, also |
| 279 | // updates the signed hash calculator with the first |signed_hash_buffer_size| |
| 280 | // bytes in |buffer_|. Then discard the content, ensuring that memory is being |
| 281 | // deallocated. If |do_advance_offset|, advances the internal offset counter |
| 282 | // accordingly. |
| 283 | void DiscardBuffer(bool do_advance_offset, size_t signed_hash_buffer_size); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 284 | |
Darin Petkov | 9b23057 | 2010-10-08 10:20:09 -0700 | [diff] [blame] | 285 | // Primes the required update state. Returns true if the update state was |
| 286 | // successfully initialized to a saved resume state or if the update is a new |
| 287 | // update. Returns false otherwise. |
| 288 | bool PrimeUpdateState(); |
| 289 | |
Sen Jiang | 08c6da1 | 2019-01-07 18:28:56 -0800 | [diff] [blame] | 290 | // Get the public key to be used to verify metadata signature or payload |
| 291 | // signature. Always use |public_key_path_| if exists, otherwise if the Omaha |
| 292 | // response contains a public RSA key and we're allowed to use it (e.g. if |
| 293 | // we're in developer mode), decode the key from the response and store it in |
| 294 | // |out_public_key|. Returns false on failures. |
| 295 | bool GetPublicKey(std::string* out_public_key); |
David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 296 | |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 297 | // Creates a PayloadVerifier from the zip file containing certificates. If the |
| 298 | // path to the zip file doesn't exist, falls back to use the public key. |
| 299 | // Returns a tuple with the created PayloadVerifier and if we should perform |
| 300 | // the verification. |
| 301 | std::pair<std::unique_ptr<PayloadVerifier>, bool> CreatePayloadVerifier(); |
| 302 | |
Yifan Hong | 9acd9cb | 2018-10-19 14:52:45 -0700 | [diff] [blame] | 303 | // After install_plan_ is filled with partition names and sizes, initialize |
| 304 | // metadata of partitions and map necessary devices before opening devices. |
Yifan Hong | b9d6357 | 2020-01-09 17:50:46 -0800 | [diff] [blame] | 305 | // Also see comment for the static PreparePartitionsForUpdate(). |
Daniel Zheng | eede4c8 | 2023-06-13 11:21:06 -0700 | [diff] [blame] | 306 | bool PreparePartitionsForUpdate(uint64_t* required_size, |
| 307 | ErrorCode* error = nullptr); |
Yifan Hong | 9acd9cb | 2018-10-19 14:52:45 -0700 | [diff] [blame] | 308 | |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 309 | // Check if current manifest contains timestamp errors. |
| 310 | // Return: |
| 311 | // - kSuccess if update is valid. |
| 312 | // - kPayloadTimestampError if downgrade is detected |
| 313 | // - kDownloadManifestParseError if |new_version| has an incorrect format |
| 314 | // - Other error values if the source of error is known, or kError for |
| 315 | // a generic error on the device. |
| 316 | ErrorCode CheckTimestampError() const; |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 317 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 318 | // Check if partition `part_name` is a dynamic partition. |
Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 319 | bool IsDynamicPartition(const std::string& part_name, uint32_t slot); |
Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 320 | |
Kelvin Zhang | bdba354 | 2023-02-24 11:08:55 -0800 | [diff] [blame] | 321 | bool CheckSPLDowngrade(); |
Kelvin Zhang | cfc531f | 2022-08-24 17:58:53 +0000 | [diff] [blame] | 322 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 323 | // Update Engine preference store. |
| 324 | PrefsInterface* prefs_; |
| 325 | |
Alex Deymo | 542c19b | 2015-12-03 07:43:31 -0300 | [diff] [blame] | 326 | // BootControl and Hardware interface references. |
| 327 | BootControlInterface* boot_control_; |
| 328 | HardwareInterface* hardware_; |
| 329 | |
| 330 | // The DownloadActionDelegate instance monitoring the DownloadAction, or a |
| 331 | // nullptr if not used. |
| 332 | DownloadActionDelegate* download_delegate_; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 333 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 334 | // Install Plan based on Omaha Response. |
| 335 | InstallPlan* install_plan_; |
| 336 | |
Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 337 | // Pointer to the current payload in install_plan_.payloads. |
| 338 | InstallPlan::Payload* payload_{nullptr}; |
| 339 | |
Sen Jiang | b5f601d | 2018-02-02 13:51:21 -0800 | [diff] [blame] | 340 | PayloadMetadata payload_metadata_; |
| 341 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 342 | // Parsed manifest. Set after enough bytes to parse the manifest were |
| 343 | // downloaded. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 344 | DeltaArchiveManifest manifest_; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 345 | bool manifest_parsed_{false}; |
| 346 | bool manifest_valid_{false}; |
| 347 | uint64_t metadata_size_{0}; |
Sen Jiang | 76bfa74 | 2015-10-12 17:13:26 -0700 | [diff] [blame] | 348 | uint32_t metadata_signature_size_{0}; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 349 | uint64_t major_payload_version_{0}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 350 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 351 | // Accumulated number of operations per partition. The i-th element is the |
| 352 | // sum of the number of operations for all the partitions from 0 to i |
| 353 | // inclusive. Valid when |manifest_valid_| is true. |
| 354 | std::vector<size_t> acc_num_operations_; |
| 355 | |
| 356 | // The total operations in a payload. Valid when |manifest_valid_| is true, |
| 357 | // otherwise 0. |
| 358 | size_t num_total_operations_{0}; |
| 359 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 360 | // The list of partitions to update as found in the manifest major |
| 361 | // version 2. When parsing an older manifest format, the information is |
| 362 | // converted over to this format instead. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 363 | std::vector<PartitionUpdate> partitions_; |
| 364 | |
| 365 | // Index in the list of partitions (|partitions_| member) of the current |
| 366 | // partition being processed. |
| 367 | size_t current_partition_{0}; |
| 368 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 369 | // Index of the next operation to perform in the manifest. The index is |
| 370 | // linear on the total number of operation on the manifest. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 371 | size_t next_operation_num_{0}; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 372 | |
Gilad Arnold | fe13393 | 2014-01-14 12:25:50 -0800 | [diff] [blame] | 373 | // A buffer used for accumulating downloaded data. Initially, it stores the |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 374 | // payload metadata; once that's downloaded and parsed, it stores data for |
| 375 | // the next update operation. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 376 | brillo::Blob buffer_; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 377 | // Offset of buffer_ in the binary blobs section of the update. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 378 | uint64_t buffer_offset_{0}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 379 | |
Kelvin Zhang | cfe694f | 2020-11-13 13:10:42 -0500 | [diff] [blame] | 380 | // Last |next_operation_num_| value updated as part of the progress update. |
| 381 | uint64_t last_updated_operation_num_{std::numeric_limits<uint64_t>::max()}; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 382 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 383 | // The block size (parsed from the manifest). |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 384 | uint32_t block_size_{0}; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 385 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 386 | // Calculates the whole payload file hash, including headers and signatures. |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 387 | HashCalculator payload_hash_calculator_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 388 | |
Sen Jiang | f681380 | 2015-11-03 21:27:29 -0800 | [diff] [blame] | 389 | // Calculates the hash of the portion of the payload signed by the payload |
| 390 | // signature. This hash skips the metadata signature portion, located after |
| 391 | // the metadata and doesn't include the payload signature itself. |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 392 | HashCalculator signed_hash_calculator_; |
Darin Petkov | 437adc4 | 2010-10-07 13:12:24 -0700 | [diff] [blame] | 393 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 394 | // Signatures message blob extracted directly from the payload. |
Sen Jiang | 9b2f178 | 2019-01-24 14:27:50 -0800 | [diff] [blame] | 395 | std::string signatures_message_data_; |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 396 | |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 397 | // The public key to be used. Provided as a member so that tests can |
| 398 | // override with test keys. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 399 | std::string public_key_path_{constants::kUpdatePayloadPublicKeyPath}; |
Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 400 | |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 401 | // The path to the zip file with X509 certificates. |
Kelvin Zhang | 1304fe7 | 2021-10-06 19:12:12 -0700 | [diff] [blame] | 402 | const std::string update_certificates_path_; |
Tianjie Xu | 7a78d63 | 2019-10-08 16:32:39 -0700 | [diff] [blame] | 403 | |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 404 | // The number of bytes received so far, used for progress tracking. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 405 | size_t total_bytes_received_{0}; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 406 | |
| 407 | // An overall progress counter, which should reflect both download progress |
| 408 | // and the ratio of applied operations. Range is 0-100. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 409 | unsigned overall_progress_{0}; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 410 | |
| 411 | // The last progress chunk recorded. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 412 | unsigned last_progress_chunk_{0}; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 413 | |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 414 | // If |true|, the update is user initiated (vs. periodic update checks). |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 415 | bool interactive_{false}; |
Amin Hassani | 7ecda26 | 2017-07-11 17:10:50 -0700 | [diff] [blame] | 416 | |
Kelvin Zhang | 94f51cc | 2020-09-25 11:34:49 -0400 | [diff] [blame] | 417 | // The timeout after which we should force emitting a progress log |
| 418 | // (constant), and the actual point in time for the next forced log to be |
| 419 | // emitted. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 420 | const base::TimeDelta forced_progress_log_wait_{ |
| 421 | base::TimeDelta::FromSeconds(kProgressLogTimeoutSeconds)}; |
Sen Jiang | 53c2ec5 | 2019-01-10 15:27:59 -0800 | [diff] [blame] | 422 | base::TimeTicks forced_progress_log_time_; |
Gilad Arnold | 8a86fa5 | 2013-01-15 12:35:05 -0800 | [diff] [blame] | 423 | |
Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 424 | // The frequency that we should write an update checkpoint (constant), and |
| 425 | // the point in time at which the next checkpoint should be written. |
| 426 | const base::TimeDelta update_checkpoint_wait_{ |
| 427 | base::TimeDelta::FromSeconds(kCheckpointFrequencySeconds)}; |
Sen Jiang | 53c2ec5 | 2019-01-10 15:27:59 -0800 | [diff] [blame] | 428 | base::TimeTicks update_checkpoint_time_; |
Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 429 | |
Kelvin Zhang | e52b6cd | 2021-02-09 15:28:40 -0500 | [diff] [blame] | 430 | std::unique_ptr<PartitionWriterInterface> partition_writer_; |
Kelvin Zhang | 9bd519d | 2020-09-23 12:55:19 -0400 | [diff] [blame] | 431 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 432 | DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); |
| 433 | }; |
| 434 | |
| 435 | } // namespace chromeos_update_engine |
| 436 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 437 | #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_DELTA_PERFORMER_H_ |