blob: c693333a1ddcd71842027457d1187a2fa2a19e91 [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 Petkov73058b42010-10-06 16:32:19 -070034 DeltaPerformer(PrefsInterface* prefs)
35 : prefs_(prefs),
36 fd_(-1),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070037 kernel_fd_(-1),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070038 manifest_valid_(false),
Darin Petkov437adc42010-10-07 13:12:24 -070039 manifest_metadata_size_(0),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070040 next_operation_num_(0),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070041 buffer_offset_(0),
Darin Petkov0406e402010-10-06 21:33:11 -070042 last_updated_buffer_offset_(kuint64max),
Darin Petkov3aefa862010-12-07 14:45:00 -080043 block_size_(0) {}
Darin Petkovd7061ab2010-10-06 14:37:09 -070044
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070045 // Opens the kernel. Should be called before or after Open(), but before
46 // Write(). The kernel file will be close()d when Close() is called.
47 bool OpenKernel(const char* kernel_path);
48
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070049 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be
50 // Open()ed again.
51 int Open(const char* path, int flags, mode_t mode);
52
53 // Wrapper around write. Returns bytes written on success or
54 // -errno on error.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070055 ssize_t Write(const void* bytes, size_t count);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070056
57 // Wrapper around close. Returns 0 on success or -errno on error.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070058 // Closes both 'path' given to Open() and the kernel path.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070059 int Close();
Darin Petkovd7061ab2010-10-06 14:37:09 -070060
61 // Verifies the downloaded payload against the signed hash included in the
Darin Petkov437adc42010-10-07 13:12:24 -070062 // payload as well as against the update check hash and size and returns true
63 // on success, false on failure. This method should be called after closing
64 // the stream. Note this method skips the signed hash check if the public key
65 // is unavailable; it returns false if the public key is available but the
66 // delta payload doesn't include a signature. If |public_key_path| is an empty
67 // string, uses the default public key path.
68 bool VerifyPayload(const std::string& public_key_path,
69 const std::string& update_check_response_hash,
70 const uint64_t update_check_response_size);
Darin Petkovd7061ab2010-10-06 14:37:09 -070071
Darin Petkov3aefa862010-12-07 14:45:00 -080072 // Reads from the update manifest the expected sizes and hashes of the target
73 // kernel and rootfs partitions. These values can be used for applied update
74 // hash verification. This method must be called after the update manifest has
75 // been parsed (e.g., after closing the stream). Returns true on success, and
76 // false on failure (e.g., when the values are not present in the update
77 // manifest).
78 bool GetNewPartitionInfo(uint64_t* kernel_size,
79 std::vector<char>* kernel_hash,
80 uint64_t* rootfs_size,
81 std::vector<char>* rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -070082
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070083 // Converts an ordered collection of Extent objects which contain data of
84 // length full_length to a comma-separated string. For each Extent, the
85 // string will have the start offset and then the length in bytes.
86 // The length value of the last extent in the string may be short, since
87 // the full length of all extents in the string is capped to full_length.
88 // Also, an extent starting at kSparseHole, appears as -1 in the string.
89 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
90 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
91 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
92 static bool ExtentsToBsdiffPositionsString(
93 const google::protobuf::RepeatedPtrField<Extent>& extents,
94 uint64_t block_size,
95 uint64_t full_length,
96 std::string* positions_string);
97
Darin Petkov0406e402010-10-06 21:33:11 -070098 // Returns true if a previous update attempt can be continued based on the
99 // persistent preferences and the new update check response hash.
100 static bool CanResumeUpdate(PrefsInterface* prefs,
101 std::string update_check_response_hash);
102
103 // Resets the persistent update progress state to indicate that an update
Darin Petkov9b230572010-10-08 10:20:09 -0700104 // can't be resumed. Performs a quick update-in-progress reset if |quick| is
105 // true, otherwise resets all progress-related update state. Returns true on
106 // success, false otherwise.
107 static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
Darin Petkov0406e402010-10-06 21:33:11 -0700108
Darin Petkov9574f7e2011-01-13 10:48:12 -0800109 // Attempts to parse the update metadata starting from the beginning of
110 // |payload| into |manifest|. On success, sets |metadata_size| to the total
111 // metadata bytes (including the delta magic and metadata size fields), and
112 // returns kMetadataParseSuccess. Returns kMetadataParseInsufficientData if
113 // more data is needed to parse the complete metadata. Returns
114 // kMetadataParseError if the metadata can't be parsed given the payload.
115 static MetadataParseResult ParsePayloadMetadata(
116 const std::vector<char>& payload,
117 DeltaArchiveManifest* manifest,
118 uint64_t* metadata_size);
119
Darin Petkov3aefa862010-12-07 14:45:00 -0800120 void set_current_kernel_hash(const std::vector<char>& hash) {
Darin Petkov698d0412010-10-13 10:59:44 -0700121 current_kernel_hash_ = hash;
122 }
123
Darin Petkov3aefa862010-12-07 14:45:00 -0800124 void set_current_rootfs_hash(const std::vector<char>& hash) {
Darin Petkov698d0412010-10-13 10:59:44 -0700125 current_rootfs_hash_ = hash;
126 }
127
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700128 private:
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700129 friend class DeltaPerformerTest;
130 FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
131
132 static bool IsIdempotentOperation(
133 const DeltaArchiveManifest_InstallOperation& op);
134
Darin Petkov698d0412010-10-13 10:59:44 -0700135 // Verifies that the expected source partition hashes (if present) match the
136 // hashes for the current partitions. Returns true if there're no expected
137 // hashes in the payload (e.g., if it's a new-style full update) or if the
138 // hashes match; returns false otherwise.
139 bool VerifySourcePartitions();
140
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700141 // Returns true if enough of the delta file has been passed via Write()
142 // to be able to perform a given install operation.
143 bool CanPerformInstallOperation(
144 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700145
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700146 // Returns true on success.
147 bool PerformInstallOperation(
148 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700149
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700150 // These perform a specific type of operation and return true on success.
151 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700152 const DeltaArchiveManifest_InstallOperation& operation,
153 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700154 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700155 const DeltaArchiveManifest_InstallOperation& operation,
156 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700157 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700158 const DeltaArchiveManifest_InstallOperation& operation,
159 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700160
Darin Petkovd7061ab2010-10-06 14:37:09 -0700161 // Returns true if the payload signature message has been extracted from
162 // |operation|, false otherwise.
163 bool ExtractSignatureMessage(
164 const DeltaArchiveManifest_InstallOperation& operation);
165
Darin Petkov437adc42010-10-07 13:12:24 -0700166 // Updates the hash calculator with |count| bytes at the head of |buffer_| and
167 // then discards them.
168 void DiscardBufferHeadBytes(size_t count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700169
Darin Petkov0406e402010-10-06 21:33:11 -0700170 // Checkpoints the update progress into persistent storage to allow this
171 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700172 bool CheckpointUpdateProgress();
173
Darin Petkov9b230572010-10-08 10:20:09 -0700174 // Primes the required update state. Returns true if the update state was
175 // successfully initialized to a saved resume state or if the update is a new
176 // update. Returns false otherwise.
177 bool PrimeUpdateState();
178
Darin Petkov73058b42010-10-06 16:32:19 -0700179 // Update Engine preference store.
180 PrefsInterface* prefs_;
181
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700182 // File descriptor of open device.
183 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700184
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700185 // File descriptor of the kernel device
186 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700187
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700188 std::string path_; // Path that fd_ refers to.
189 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700190
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700191 DeltaArchiveManifest manifest_;
192 bool manifest_valid_;
Darin Petkov437adc42010-10-07 13:12:24 -0700193 uint64_t manifest_metadata_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700194
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700195 // Index of the next operation to perform in the manifest.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700196 int next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700197
198 // buffer_ is a window of the data that's been downloaded. At first,
199 // it contains the beginning of the download, but after the protobuf
200 // has been downloaded and parsed, it contains a sliding window of
201 // data blobs.
202 std::vector<char> buffer_;
203 // Offset of buffer_ in the binary blobs section of the update.
204 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700205
Darin Petkov0406e402010-10-06 21:33:11 -0700206 // Last |buffer_offset_| value updated as part of the progress update.
207 uint64_t last_updated_buffer_offset_;
208
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700209 // The block size (parsed from the manifest).
210 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700211
Darin Petkov437adc42010-10-07 13:12:24 -0700212 // Calculates the payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700213 OmahaHashCalculator hash_calculator_;
214
Darin Petkov437adc42010-10-07 13:12:24 -0700215 // Saves the signed hash context.
216 std::string signed_hash_context_;
217
Darin Petkovd7061ab2010-10-06 14:37:09 -0700218 // Signatures message blob extracted directly from the payload.
219 std::vector<char> signatures_message_data_;
220
Darin Petkov698d0412010-10-13 10:59:44 -0700221 // Hashes for the current partitions to be used for source partition
222 // verification.
Darin Petkov3aefa862010-12-07 14:45:00 -0800223 std::vector<char> current_kernel_hash_;
224 std::vector<char> current_rootfs_hash_;
Darin Petkov698d0412010-10-13 10:59:44 -0700225
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700226 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
227};
228
229} // namespace chromeos_update_engine
230
231#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__