blob: 8a9914d4c77627f612c4ed00f0cb4c351feb2f23 [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 Petkov3aefa862010-12-07 14:45:00 -080037 block_size_(0) {}
Darin Petkovd7061ab2010-10-06 14:37:09 -070038
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070039 // Opens the kernel. Should be called before or after Open(), but before
40 // Write(). The kernel file will be close()d when Close() is called.
41 bool OpenKernel(const char* kernel_path);
42
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070043 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be
44 // Open()ed again.
45 int Open(const char* path, int flags, mode_t mode);
46
47 // Wrapper around write. Returns bytes written on success or
48 // -errno on error.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070049 ssize_t Write(const void* bytes, size_t count);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070050
51 // Wrapper around close. Returns 0 on success or -errno on error.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070052 // Closes both 'path' given to Open() and the kernel path.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070053 int Close();
Darin Petkovd7061ab2010-10-06 14:37:09 -070054
55 // Verifies the downloaded payload against the signed hash included in the
Darin Petkov437adc42010-10-07 13:12:24 -070056 // payload as well as against the update check hash and size and returns true
57 // on success, false on failure. This method should be called after closing
58 // the stream. Note this method skips the signed hash check if the public key
59 // is unavailable; it returns false if the public key is available but the
60 // delta payload doesn't include a signature. If |public_key_path| is an empty
61 // string, uses the default public key path.
62 bool VerifyPayload(const std::string& public_key_path,
63 const std::string& update_check_response_hash,
64 const uint64_t update_check_response_size);
Darin Petkovd7061ab2010-10-06 14:37:09 -070065
Darin Petkov3aefa862010-12-07 14:45:00 -080066 // Reads from the update manifest the expected sizes and hashes of the target
67 // kernel and rootfs partitions. These values can be used for applied update
68 // hash verification. This method must be called after the update manifest has
69 // been parsed (e.g., after closing the stream). Returns true on success, and
70 // false on failure (e.g., when the values are not present in the update
71 // manifest).
72 bool GetNewPartitionInfo(uint64_t* kernel_size,
73 std::vector<char>* kernel_hash,
74 uint64_t* rootfs_size,
75 std::vector<char>* rootfs_hash);
Darin Petkov2dd01092010-10-08 15:43:05 -070076
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070077 // Converts an ordered collection of Extent objects which contain data of
78 // length full_length to a comma-separated string. For each Extent, the
79 // string will have the start offset and then the length in bytes.
80 // The length value of the last extent in the string may be short, since
81 // the full length of all extents in the string is capped to full_length.
82 // Also, an extent starting at kSparseHole, appears as -1 in the string.
83 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
84 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
85 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
86 static bool ExtentsToBsdiffPositionsString(
87 const google::protobuf::RepeatedPtrField<Extent>& extents,
88 uint64_t block_size,
89 uint64_t full_length,
90 std::string* positions_string);
91
Darin Petkov0406e402010-10-06 21:33:11 -070092 // Returns true if a previous update attempt can be continued based on the
93 // persistent preferences and the new update check response hash.
94 static bool CanResumeUpdate(PrefsInterface* prefs,
95 std::string update_check_response_hash);
96
97 // Resets the persistent update progress state to indicate that an update
Darin Petkov9b230572010-10-08 10:20:09 -070098 // can't be resumed. Performs a quick update-in-progress reset if |quick| is
99 // true, otherwise resets all progress-related update state. Returns true on
100 // success, false otherwise.
101 static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
Darin Petkov0406e402010-10-06 21:33:11 -0700102
Darin Petkov3aefa862010-12-07 14:45:00 -0800103 void set_current_kernel_hash(const std::vector<char>& hash) {
Darin Petkov698d0412010-10-13 10:59:44 -0700104 current_kernel_hash_ = hash;
105 }
106
Darin Petkov3aefa862010-12-07 14:45:00 -0800107 void set_current_rootfs_hash(const std::vector<char>& hash) {
Darin Petkov698d0412010-10-13 10:59:44 -0700108 current_rootfs_hash_ = hash;
109 }
110
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700111 private:
Andrew de los Reyes353777c2010-10-08 10:34:30 -0700112 friend class DeltaPerformerTest;
113 FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
114
115 static bool IsIdempotentOperation(
116 const DeltaArchiveManifest_InstallOperation& op);
117
Darin Petkov698d0412010-10-13 10:59:44 -0700118 // Verifies that the expected source partition hashes (if present) match the
119 // hashes for the current partitions. Returns true if there're no expected
120 // hashes in the payload (e.g., if it's a new-style full update) or if the
121 // hashes match; returns false otherwise.
122 bool VerifySourcePartitions();
123
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700124 // Returns true if enough of the delta file has been passed via Write()
125 // to be able to perform a given install operation.
126 bool CanPerformInstallOperation(
127 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700128
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700129 // Returns true on success.
130 bool PerformInstallOperation(
131 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700132
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700133 // These perform a specific type of operation and return true on success.
134 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700135 const DeltaArchiveManifest_InstallOperation& operation,
136 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700137 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700138 const DeltaArchiveManifest_InstallOperation& operation,
139 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700140 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700141 const DeltaArchiveManifest_InstallOperation& operation,
142 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700143
Darin Petkovd7061ab2010-10-06 14:37:09 -0700144 // Returns true if the payload signature message has been extracted from
145 // |operation|, false otherwise.
146 bool ExtractSignatureMessage(
147 const DeltaArchiveManifest_InstallOperation& operation);
148
Darin Petkov437adc42010-10-07 13:12:24 -0700149 // Updates the hash calculator with |count| bytes at the head of |buffer_| and
150 // then discards them.
151 void DiscardBufferHeadBytes(size_t count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700152
Darin Petkov0406e402010-10-06 21:33:11 -0700153 // Checkpoints the update progress into persistent storage to allow this
154 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700155 bool CheckpointUpdateProgress();
156
Darin Petkov9b230572010-10-08 10:20:09 -0700157 // Primes the required update state. Returns true if the update state was
158 // successfully initialized to a saved resume state or if the update is a new
159 // update. Returns false otherwise.
160 bool PrimeUpdateState();
161
Darin Petkov73058b42010-10-06 16:32:19 -0700162 // Update Engine preference store.
163 PrefsInterface* prefs_;
164
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700165 // File descriptor of open device.
166 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700167
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700168 // File descriptor of the kernel device
169 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700170
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700171 std::string path_; // Path that fd_ refers to.
172 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700173
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700174 DeltaArchiveManifest manifest_;
175 bool manifest_valid_;
Darin Petkov437adc42010-10-07 13:12:24 -0700176 uint64_t manifest_metadata_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700177
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700178 // Index of the next operation to perform in the manifest.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700179 int next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700180
181 // buffer_ is a window of the data that's been downloaded. At first,
182 // it contains the beginning of the download, but after the protobuf
183 // has been downloaded and parsed, it contains a sliding window of
184 // data blobs.
185 std::vector<char> buffer_;
186 // Offset of buffer_ in the binary blobs section of the update.
187 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700188
Darin Petkov0406e402010-10-06 21:33:11 -0700189 // Last |buffer_offset_| value updated as part of the progress update.
190 uint64_t last_updated_buffer_offset_;
191
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700192 // The block size (parsed from the manifest).
193 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700194
Darin Petkov437adc42010-10-07 13:12:24 -0700195 // Calculates the payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700196 OmahaHashCalculator hash_calculator_;
197
Darin Petkov437adc42010-10-07 13:12:24 -0700198 // Saves the signed hash context.
199 std::string signed_hash_context_;
200
Darin Petkovd7061ab2010-10-06 14:37:09 -0700201 // Signatures message blob extracted directly from the payload.
202 std::vector<char> signatures_message_data_;
203
Darin Petkov698d0412010-10-13 10:59:44 -0700204 // Hashes for the current partitions to be used for source partition
205 // verification.
Darin Petkov3aefa862010-12-07 14:45:00 -0800206 std::vector<char> current_kernel_hash_;
207 std::vector<char> current_rootfs_hash_;
Darin Petkov698d0412010-10-13 10:59:44 -0700208
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700209 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
210};
211
212} // namespace chromeos_update_engine
213
214#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__