blob: 7394f940acacb9b7d23c92744f547ff304f80fb3 [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),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070037 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 Petkov2dd01092010-10-08 15:43:05 -070066 // Verifies that the generated update is correct based on the hashes sent by
67 // the server. Returns true on success, false otherwise.
68 bool VerifyAppliedUpdate(const std::string& path,
69 const std::string& kernel_path);
70
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070071 // Converts an ordered collection of Extent objects which contain data of
72 // length full_length to a comma-separated string. For each Extent, the
73 // string will have the start offset and then the length in bytes.
74 // The length value of the last extent in the string may be short, since
75 // the full length of all extents in the string is capped to full_length.
76 // Also, an extent starting at kSparseHole, appears as -1 in the string.
77 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
78 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
79 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
80 static bool ExtentsToBsdiffPositionsString(
81 const google::protobuf::RepeatedPtrField<Extent>& extents,
82 uint64_t block_size,
83 uint64_t full_length,
84 std::string* positions_string);
85
Darin Petkov0406e402010-10-06 21:33:11 -070086 // Returns true if a previous update attempt can be continued based on the
87 // persistent preferences and the new update check response hash.
88 static bool CanResumeUpdate(PrefsInterface* prefs,
89 std::string update_check_response_hash);
90
91 // Resets the persistent update progress state to indicate that an update
Darin Petkov9b230572010-10-08 10:20:09 -070092 // can't be resumed. Performs a quick update-in-progress reset if |quick| is
93 // true, otherwise resets all progress-related update state. Returns true on
94 // success, false otherwise.
95 static bool ResetUpdateProgress(PrefsInterface* prefs, bool quick);
Darin Petkov0406e402010-10-06 21:33:11 -070096
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070097 private:
Andrew de los Reyes353777c2010-10-08 10:34:30 -070098 friend class DeltaPerformerTest;
99 FRIEND_TEST(DeltaPerformerTest, IsIdempotentOperationTest);
100
101 static bool IsIdempotentOperation(
102 const DeltaArchiveManifest_InstallOperation& op);
103
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700104 // Returns true if enough of the delta file has been passed via Write()
105 // to be able to perform a given install operation.
106 bool CanPerformInstallOperation(
107 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700108
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700109 // Returns true on success.
110 bool PerformInstallOperation(
111 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700112
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700113 // These perform a specific type of operation and return true on success.
114 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700115 const DeltaArchiveManifest_InstallOperation& operation,
116 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700117 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700118 const DeltaArchiveManifest_InstallOperation& operation,
119 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700120 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700121 const DeltaArchiveManifest_InstallOperation& operation,
122 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700123
Darin Petkovd7061ab2010-10-06 14:37:09 -0700124 // Returns true if the payload signature message has been extracted from
125 // |operation|, false otherwise.
126 bool ExtractSignatureMessage(
127 const DeltaArchiveManifest_InstallOperation& operation);
128
Darin Petkov437adc42010-10-07 13:12:24 -0700129 // Updates the hash calculator with |count| bytes at the head of |buffer_| and
130 // then discards them.
131 void DiscardBufferHeadBytes(size_t count);
Darin Petkovd7061ab2010-10-06 14:37:09 -0700132
Darin Petkov0406e402010-10-06 21:33:11 -0700133 // Checkpoints the update progress into persistent storage to allow this
134 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700135 bool CheckpointUpdateProgress();
136
Darin Petkov9b230572010-10-08 10:20:09 -0700137 // Primes the required update state. Returns true if the update state was
138 // successfully initialized to a saved resume state or if the update is a new
139 // update. Returns false otherwise.
140 bool PrimeUpdateState();
141
Darin Petkov73058b42010-10-06 16:32:19 -0700142 // Update Engine preference store.
143 PrefsInterface* prefs_;
144
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700145 // File descriptor of open device.
146 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700147
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700148 // File descriptor of the kernel device
149 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700150
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700151 std::string path_; // Path that fd_ refers to.
152 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700153
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700154 DeltaArchiveManifest manifest_;
155 bool manifest_valid_;
Darin Petkov437adc42010-10-07 13:12:24 -0700156 uint64_t manifest_metadata_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700157
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700158 // Index of the next operation to perform in the manifest.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700159 int next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700160
161 // buffer_ is a window of the data that's been downloaded. At first,
162 // it contains the beginning of the download, but after the protobuf
163 // has been downloaded and parsed, it contains a sliding window of
164 // data blobs.
165 std::vector<char> buffer_;
166 // Offset of buffer_ in the binary blobs section of the update.
167 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700168
Darin Petkov0406e402010-10-06 21:33:11 -0700169 // Last |buffer_offset_| value updated as part of the progress update.
170 uint64_t last_updated_buffer_offset_;
171
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700172 // The block size (parsed from the manifest).
173 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700174
Darin Petkov437adc42010-10-07 13:12:24 -0700175 // Calculates the payload hash.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700176 OmahaHashCalculator hash_calculator_;
177
Darin Petkov437adc42010-10-07 13:12:24 -0700178 // Saves the signed hash context.
179 std::string signed_hash_context_;
180
Darin Petkovd7061ab2010-10-06 14:37:09 -0700181 // Signatures message blob extracted directly from the payload.
182 std::vector<char> signatures_message_data_;
183
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700184 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
185};
186
187} // namespace chromeos_update_engine
188
189#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__