blob: 0e3cb93aea18c8e3686894af6de4614f2b13c1eb [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 Petkov73058b42010-10-06 16:32:19 -070028 DeltaPerformer(PrefsInterface* prefs)
29 : prefs_(prefs),
30 fd_(-1),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070031 kernel_fd_(-1),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070032 manifest_valid_(false),
Darin Petkov437adc42010-10-07 13:12:24 -070033 manifest_metadata_size_(0),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070034 next_operation_num_(0),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070035 buffer_offset_(0),
Darin Petkov0406e402010-10-06 21:33:11 -070036 last_updated_buffer_offset_(kuint64max),
Darin Petkov698d0412010-10-13 10:59:44 -070037 block_size_(0),
38 current_kernel_hash_(NULL),
39 current_rootfs_hash_(NULL) {}
Darin Petkovd7061ab2010-10-06 14:37:09 -070040
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070041 // Opens the kernel. Should be called before or after Open(), but before
42 // Write(). The kernel file will be close()d when Close() is called.
43 bool OpenKernel(const char* kernel_path);
44
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070045 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be
46 // Open()ed again.
47 int Open(const char* path, int flags, mode_t mode);
48
49 // Wrapper around write. Returns bytes written on success or
50 // -errno on error.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070051 ssize_t Write(const void* bytes, size_t count);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070052
53 // Wrapper around close. Returns 0 on success or -errno on error.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070054 // Closes both 'path' given to Open() and the kernel path.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070055 int Close();
Darin Petkovd7061ab2010-10-06 14:37:09 -070056
57 // Verifies the downloaded payload against the signed hash included in the
Darin Petkov437adc42010-10-07 13:12:24 -070058 // payload as well as against the update check hash and size and returns true
59 // on success, false on failure. This method should be called after closing
60 // the stream. Note this method skips the signed hash check if the public key
61 // is unavailable; it returns false if the public key is available but the
62 // delta payload doesn't include a signature. If |public_key_path| is an empty
63 // string, uses the default public key path.
64 bool VerifyPayload(const std::string& public_key_path,
65 const std::string& update_check_response_hash,
66 const uint64_t update_check_response_size);
Darin Petkovd7061ab2010-10-06 14:37:09 -070067
Darin Petkov2dd01092010-10-08 15:43:05 -070068 // Verifies that the generated update is correct based on the hashes sent by
69 // the server. Returns true on success, false otherwise.
70 bool VerifyAppliedUpdate(const std::string& path,
71 const std::string& kernel_path);
72
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070073 // Converts an ordered collection of Extent objects which contain data of
74 // length full_length to a comma-separated string. For each Extent, the
75 // string will have the start offset and then the length in bytes.
76 // The length value of the last extent in the string may be short, since
77 // the full length of all extents in the string is capped to full_length.
78 // Also, an extent starting at kSparseHole, appears as -1 in the string.
79 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
80 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
81 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
82 static bool ExtentsToBsdiffPositionsString(
83 const google::protobuf::RepeatedPtrField<Extent>& extents,
84 uint64_t block_size,
85 uint64_t full_length,
86 std::string* positions_string);
87
Darin Petkov0406e402010-10-06 21:33:11 -070088 // Returns true if a previous update attempt can be continued based on the
89 // persistent preferences and the new update check response hash.
90 static bool CanResumeUpdate(PrefsInterface* prefs,
91 std::string update_check_response_hash);
92
93 // Resets the persistent update progress state to indicate that an update
Darin Petkov9b230572010-10-08 10:20:09 -070094 // can't be resumed. Performs a quick update-in-progress reset if |quick| is
95 // true, otherwise resets all progress-related update state. Returns true on
96 // success, false otherwise.
97 static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
Darin Petkov0406e402010-10-06 21:33:11 -070098
Darin Petkov698d0412010-10-13 10:59:44 -070099 void set_current_kernel_hash(const std::vector<char>* hash) {
100 current_kernel_hash_ = hash;
101 }
102
103 void set_current_rootfs_hash(const std::vector<char>* hash) {
104 current_rootfs_hash_ = hash;
105 }
106
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700107 private:
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700108 friend class DeltaPerformerTest;
109 FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
110
111 static bool IsIdempotentOperation(
112 const DeltaArchiveManifest_InstallOperation& op);
113
Darin Petkov698d0412010-10-13 10:59:44 -0700114 // Verifies that the expected source partition hashes (if present) match the
115 // hashes for the current partitions. Returns true if there're no expected
116 // hashes in the payload (e.g., if it's a new-style full update) or if the
117 // hashes match; returns false otherwise.
118 bool VerifySourcePartitions();
119
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700120 // Returns true if enough of the delta file has been passed via Write()
121 // to be able to perform a given install operation.
122 bool CanPerformInstallOperation(
123 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700124
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700125 // Returns true on success.
126 bool PerformInstallOperation(
127 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700128
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700129 // These perform a specific type of operation and return true on success.
130 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700131 const DeltaArchiveManifest_InstallOperation& operation,
132 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700133 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700134 const DeltaArchiveManifest_InstallOperation& operation,
135 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700136 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700137 const DeltaArchiveManifest_InstallOperation& operation,
138 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700139
Darin Petkovd7061ab2010-10-06 14:37:09 -0700140 // Returns true if the payload signature message has been extracted from
141 // |operation|, false otherwise.
142 bool ExtractSignatureMessage(
143 const DeltaArchiveManifest_InstallOperation& operation);
144
Darin Petkov437adc42010-10-07 13:12:24 -0700145 // Updates the hash calculator with |count| bytes at the head of |buffer_| and
146 // then discards them.
147 void DiscardBufferHeadBytes(size_t count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700148
Darin Petkov0406e402010-10-06 21:33:11 -0700149 // Checkpoints the update progress into persistent storage to allow this
150 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700151 bool CheckpointUpdateProgress();
152
Darin Petkov9b230572010-10-08 10:20:09 -0700153 // Primes the required update state. Returns true if the update state was
154 // successfully initialized to a saved resume state or if the update is a new
155 // update. Returns false otherwise.
156 bool PrimeUpdateState();
157
Darin Petkov73058b42010-10-06 16:32:19 -0700158 // Update Engine preference store.
159 PrefsInterface* prefs_;
160
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700161 // File descriptor of open device.
162 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700163
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700164 // File descriptor of the kernel device
165 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700166
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700167 std::string path_; // Path that fd_ refers to.
168 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700169
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700170 DeltaArchiveManifest manifest_;
171 bool manifest_valid_;
Darin Petkov437adc42010-10-07 13:12:24 -0700172 uint64_t manifest_metadata_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700173
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700174 // Index of the next operation to perform in the manifest.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700175 int next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700176
177 // buffer_ is a window of the data that's been downloaded. At first,
178 // it contains the beginning of the download, but after the protobuf
179 // has been downloaded and parsed, it contains a sliding window of
180 // data blobs.
181 std::vector<char> buffer_;
182 // Offset of buffer_ in the binary blobs section of the update.
183 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700184
Darin Petkov0406e402010-10-06 21:33:11 -0700185 // Last |buffer_offset_| value updated as part of the progress update.
186 uint64_t last_updated_buffer_offset_;
187
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700188 // The block size (parsed from the manifest).
189 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700190
Darin Petkov437adc42010-10-07 13:12:24 -0700191 // Calculates the payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700192 OmahaHashCalculator hash_calculator_;
193
Darin Petkov437adc42010-10-07 13:12:24 -0700194 // Saves the signed hash context.
195 std::string signed_hash_context_;
196
Darin Petkovd7061ab2010-10-06 14:37:09 -0700197 // Signatures message blob extracted directly from the payload.
198 std::vector<char> signatures_message_data_;
199
Darin Petkov698d0412010-10-13 10:59:44 -0700200 // Hashes for the current partitions to be used for source partition
201 // verification.
202 const std::vector<char>* current_kernel_hash_;
203 const std::vector<char>* current_rootfs_hash_;
204
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700205 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
206};
207
208} // namespace chromeos_update_engine
209
210#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__