blob: 7cc1d9bf7378804fda2fb8a3b12dc30d75b3174e [file] [log] [blame]
Andrew de los Reyes09e56d62010-04-23 13:45:53 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__
7
8#include <inttypes.h>
Darin Petkovd7061ab2010-10-06 14:37:09 -07009
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070010#include <vector>
Darin Petkovd7061ab2010-10-06 14:37:09 -070011
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070012#include <google/protobuf/repeated_field.h>
Andrew de los Reyes353777c2010-10-08 10:34:30 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd7061ab2010-10-06 14:37:09 -070014
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070015#include "update_engine/file_writer.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070016#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070017#include "update_engine/update_metadata.pb.h"
18
19namespace chromeos_update_engine {
20
Darin Petkov73058b42010-10-06 16:32:19 -070021class PrefsInterface;
22
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070023// This class performs the actions in a delta update synchronously. The delta
24// update itself should be passed in in chunks as it is received.
25
26class DeltaPerformer : public FileWriter {
27 public:
Darin Petkov9574f7e2011-01-13 10:48:12 -080028 enum MetadataParseResult {
29 kMetadataParseSuccess,
30 kMetadataParseError,
31 kMetadataParseInsufficientData,
32 };
33
Darin Petkovabc7bc02011-02-23 14:39:43 -080034 static const char kUpdatePayloadPublicKeyPath[];
35
Darin Petkov73058b42010-10-06 16:32:19 -070036 DeltaPerformer(PrefsInterface* prefs)
37 : prefs_(prefs),
38 fd_(-1),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070039 kernel_fd_(-1),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070040 manifest_valid_(false),
Darin Petkov437adc42010-10-07 13:12:24 -070041 manifest_metadata_size_(0),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070042 next_operation_num_(0),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070043 buffer_offset_(0),
Darin Petkov0406e402010-10-06 21:33:11 -070044 last_updated_buffer_offset_(kuint64max),
Darin Petkov3aefa862010-12-07 14:45:00 -080045 block_size_(0) {}
Darin Petkovd7061ab2010-10-06 14:37:09 -070046
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070047 // Opens the kernel. Should be called before or after Open(), but before
48 // Write(). The kernel file will be close()d when Close() is called.
49 bool OpenKernel(const char* kernel_path);
50
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070051 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be
52 // Open()ed again.
53 int Open(const char* path, int flags, mode_t mode);
54
55 // Wrapper around write. Returns bytes written on success or
56 // -errno on error.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070057 ssize_t Write(const void* bytes, size_t count);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070058
59 // Wrapper around close. Returns 0 on success or -errno on error.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070060 // Closes both 'path' given to Open() and the kernel path.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070061 int Close();
Darin Petkovd7061ab2010-10-06 14:37:09 -070062
63 // Verifies the downloaded payload against the signed hash included in the
Darin Petkov437adc42010-10-07 13:12:24 -070064 // payload as well as against the update check hash and size and returns true
65 // on success, false on failure. This method should be called after closing
66 // the stream. Note this method skips the signed hash check if the public key
67 // is unavailable; it returns false if the public key is available but the
68 // delta payload doesn't include a signature. If |public_key_path| is an empty
69 // string, uses the default public key path.
70 bool VerifyPayload(const std::string& public_key_path,
71 const std::string& update_check_response_hash,
72 const uint64_t update_check_response_size);
Darin Petkovd7061ab2010-10-06 14:37:09 -070073
Darin Petkov3aefa862010-12-07 14:45:00 -080074 // Reads from the update manifest the expected sizes and hashes of the target
75 // kernel and rootfs partitions. These values can be used for applied update
76 // hash verification. This method must be called after the update manifest has
77 // been parsed (e.g., after closing the stream). Returns true on success, and
78 // false on failure (e.g., when the values are not present in the update
79 // manifest).
80 bool GetNewPartitionInfo(uint64_t* kernel_size,
81 std::vector<char>* kernel_hash,
82 uint64_t* rootfs_size,
83 std::vector<char>* rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -070084
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070085 // Converts an ordered collection of Extent objects which contain data of
86 // length full_length to a comma-separated string. For each Extent, the
87 // string will have the start offset and then the length in bytes.
88 // The length value of the last extent in the string may be short, since
89 // the full length of all extents in the string is capped to full_length.
90 // Also, an extent starting at kSparseHole, appears as -1 in the string.
91 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
92 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
93 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
94 static bool ExtentsToBsdiffPositionsString(
95 const google::protobuf::RepeatedPtrField<Extent>& extents,
96 uint64_t block_size,
97 uint64_t full_length,
98 std::string* positions_string);
99
Darin Petkov0406e402010-10-06 21:33:11 -0700100 // Returns true if a previous update attempt can be continued based on the
101 // persistent preferences and the new update check response hash.
102 static bool CanResumeUpdate(PrefsInterface* prefs,
103 std::string update_check_response_hash);
104
105 // Resets the persistent update progress state to indicate that an update
Darin Petkov9b230572010-10-08 10:20:09 -0700106 // can't be resumed. Performs a quick update-in-progress reset if |quick| is
107 // true, otherwise resets all progress-related update state. Returns true on
108 // success, false otherwise.
109 static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
Darin Petkov0406e402010-10-06 21:33:11 -0700110
Darin Petkov9574f7e2011-01-13 10:48:12 -0800111 // Attempts to parse the update metadata starting from the beginning of
112 // |payload| into |manifest|. On success, sets |metadata_size| to the total
113 // metadata bytes (including the delta magic and metadata size fields), and
114 // returns kMetadataParseSuccess. Returns kMetadataParseInsufficientData if
115 // more data is needed to parse the complete metadata. Returns
116 // kMetadataParseError if the metadata can't be parsed given the payload.
117 static MetadataParseResult ParsePayloadMetadata(
118 const std::vector<char>& payload,
119 DeltaArchiveManifest* manifest,
120 uint64_t* metadata_size);
121
Darin Petkov3aefa862010-12-07 14:45:00 -0800122 void set_current_kernel_hash(const std::vector<char>& hash) {
Darin Petkov698d0412010-10-13 10:59:44 -0700123 current_kernel_hash_ = hash;
124 }
125
Darin Petkov3aefa862010-12-07 14:45:00 -0800126 void set_current_rootfs_hash(const std::vector<char>& hash) {
Darin Petkov698d0412010-10-13 10:59:44 -0700127 current_rootfs_hash_ = hash;
128 }
129
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700130 private:
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700131 friend class DeltaPerformerTest;
132 FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
133
134 static bool IsIdempotentOperation(
135 const DeltaArchiveManifest_InstallOperation& op);
136
Darin Petkov698d0412010-10-13 10:59:44 -0700137 // Verifies that the expected source partition hashes (if present) match the
138 // hashes for the current partitions. Returns true if there're no expected
139 // hashes in the payload (e.g., if it's a new-style full update) or if the
140 // hashes match; returns false otherwise.
141 bool VerifySourcePartitions();
142
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700143 // Returns true if enough of the delta file has been passed via Write()
144 // to be able to perform a given install operation.
145 bool CanPerformInstallOperation(
146 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700147
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700148 // Returns true on success.
149 bool PerformInstallOperation(
150 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700151
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700152 // These perform a specific type of operation and return true on success.
153 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700154 const DeltaArchiveManifest_InstallOperation& operation,
155 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700156 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700157 const DeltaArchiveManifest_InstallOperation& operation,
158 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700159 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700160 const DeltaArchiveManifest_InstallOperation& operation,
161 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700162
Darin Petkovd7061ab2010-10-06 14:37:09 -0700163 // Returns true if the payload signature message has been extracted from
164 // |operation|, false otherwise.
165 bool ExtractSignatureMessage(
166 const DeltaArchiveManifest_InstallOperation& operation);
167
Darin Petkov437adc42010-10-07 13:12:24 -0700168 // Updates the hash calculator with |count| bytes at the head of |buffer_| and
169 // then discards them.
170 void DiscardBufferHeadBytes(size_t count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700171
Darin Petkov0406e402010-10-06 21:33:11 -0700172 // Checkpoints the update progress into persistent storage to allow this
173 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700174 bool CheckpointUpdateProgress();
175
Darin Petkov9b230572010-10-08 10:20:09 -0700176 // Primes the required update state. Returns true if the update state was
177 // successfully initialized to a saved resume state or if the update is a new
178 // update. Returns false otherwise.
179 bool PrimeUpdateState();
180
Darin Petkov73058b42010-10-06 16:32:19 -0700181 // Update Engine preference store.
182 PrefsInterface* prefs_;
183
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700184 // File descriptor of open device.
185 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700186
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700187 // File descriptor of the kernel device
188 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700189
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700190 std::string path_; // Path that fd_ refers to.
191 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700192
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700193 DeltaArchiveManifest manifest_;
194 bool manifest_valid_;
Darin Petkov437adc42010-10-07 13:12:24 -0700195 uint64_t manifest_metadata_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700196
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700197 // Index of the next operation to perform in the manifest.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700198 int next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700199
200 // buffer_ is a window of the data that's been downloaded. At first,
201 // it contains the beginning of the download, but after the protobuf
202 // has been downloaded and parsed, it contains a sliding window of
203 // data blobs.
204 std::vector<char> buffer_;
205 // Offset of buffer_ in the binary blobs section of the update.
206 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700207
Darin Petkov0406e402010-10-06 21:33:11 -0700208 // Last |buffer_offset_| value updated as part of the progress update.
209 uint64_t last_updated_buffer_offset_;
210
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700211 // The block size (parsed from the manifest).
212 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700213
Darin Petkov437adc42010-10-07 13:12:24 -0700214 // Calculates the payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700215 OmahaHashCalculator hash_calculator_;
216
Darin Petkov437adc42010-10-07 13:12:24 -0700217 // Saves the signed hash context.
218 std::string signed_hash_context_;
219
Darin Petkovd7061ab2010-10-06 14:37:09 -0700220 // Signatures message blob extracted directly from the payload.
221 std::vector<char> signatures_message_data_;
222
Darin Petkov698d0412010-10-13 10:59:44 -0700223 // Hashes for the current partitions to be used for source partition
224 // verification.
Darin Petkov3aefa862010-12-07 14:45:00 -0800225 std::vector<char> current_kernel_hash_;
226 std::vector<char> current_rootfs_hash_;
Darin Petkov698d0412010-10-13 10:59:44 -0700227
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700228 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
229};
230
231} // namespace chromeos_update_engine
232
233#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__