blob: 87a27dd9454aa7759430539c87ac3ccc8bd801b2 [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>
Darin Petkovd7061ab2010-10-06 14:37:09 -070013
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070014#include "update_engine/file_writer.h"
Darin Petkovd7061ab2010-10-06 14:37:09 -070015#include "update_engine/omaha_hash_calculator.h"
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070016#include "update_engine/update_metadata.pb.h"
17
18namespace chromeos_update_engine {
19
Darin Petkov73058b42010-10-06 16:32:19 -070020class PrefsInterface;
21
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070022// This class performs the actions in a delta update synchronously. The delta
23// update itself should be passed in in chunks as it is received.
24
25class DeltaPerformer : public FileWriter {
26 public:
Darin Petkov73058b42010-10-06 16:32:19 -070027 DeltaPerformer(PrefsInterface* prefs)
28 : prefs_(prefs),
29 fd_(-1),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070030 kernel_fd_(-1),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070031 manifest_valid_(false),
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070032 next_operation_num_(0),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070033 buffer_offset_(0),
Darin Petkov0406e402010-10-06 21:33:11 -070034 last_updated_buffer_offset_(kuint64max),
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070035 block_size_(0) {}
Darin Petkovd7061ab2010-10-06 14:37:09 -070036
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070037 // Opens the kernel. Should be called before or after Open(), but before
38 // Write(). The kernel file will be close()d when Close() is called.
39 bool OpenKernel(const char* kernel_path);
40
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070041 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be
42 // Open()ed again.
43 int Open(const char* path, int flags, mode_t mode);
44
45 // Wrapper around write. Returns bytes written on success or
46 // -errno on error.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070047 ssize_t Write(const void* bytes, size_t count);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070048
49 // Wrapper around close. Returns 0 on success or -errno on error.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070050 // Closes both 'path' given to Open() and the kernel path.
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070051 int Close();
Darin Petkovd7061ab2010-10-06 14:37:09 -070052
53 // Verifies the downloaded payload against the signed hash included in the
54 // payload and returns true on success, false on failure. This method should
55 // be called after closing the stream. Note this method returns true if the
56 // public key is unavailable; it returns false if the public key is available
57 // but the delta payload doesn't include a signature. If |public_key_path| is
58 // an empty string, uses the default public key path.
59 bool VerifyPayload(const std::string& public_key_path);
60
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070061 // Converts an ordered collection of Extent objects which contain data of
62 // length full_length to a comma-separated string. For each Extent, the
63 // string will have the start offset and then the length in bytes.
64 // The length value of the last extent in the string may be short, since
65 // the full length of all extents in the string is capped to full_length.
66 // Also, an extent starting at kSparseHole, appears as -1 in the string.
67 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1},
68 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13,
69 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083"
70 static bool ExtentsToBsdiffPositionsString(
71 const google::protobuf::RepeatedPtrField<Extent>& extents,
72 uint64_t block_size,
73 uint64_t full_length,
74 std::string* positions_string);
75
Darin Petkov0406e402010-10-06 21:33:11 -070076 // Returns true if a previous update attempt can be continued based on the
77 // persistent preferences and the new update check response hash.
78 static bool CanResumeUpdate(PrefsInterface* prefs,
79 std::string update_check_response_hash);
80
81 // Resets the persistent update progress state to indicate that an update
82 // can't be resumed. Returns true on success, false otherwise.
83 static bool ResetUpdateProgress(PrefsInterface* prefs);
84
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070085 private:
86 // Returns true if enough of the delta file has been passed via Write()
87 // to be able to perform a given install operation.
88 bool CanPerformInstallOperation(
89 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -070090
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070091 // Returns true on success.
92 bool PerformInstallOperation(
93 const DeltaArchiveManifest_InstallOperation& operation);
Darin Petkovd7061ab2010-10-06 14:37:09 -070094
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070095 // These perform a specific type of operation and return true on success.
96 bool PerformReplaceOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -070097 const DeltaArchiveManifest_InstallOperation& operation,
98 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070099 bool PerformMoveOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700100 const DeltaArchiveManifest_InstallOperation& operation,
101 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700102 bool PerformBsdiffOperation(
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700103 const DeltaArchiveManifest_InstallOperation& operation,
104 bool is_kernel_partition);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700105
Darin Petkovd7061ab2010-10-06 14:37:09 -0700106 // Returns true if the payload signature message has been extracted from
107 // |operation|, false otherwise.
108 bool ExtractSignatureMessage(
109 const DeltaArchiveManifest_InstallOperation& operation);
110
111 // Discard |count| bytes from the beginning of buffer_. If |do_hash| is true,
112 // updates the hash calculator with these bytes before discarding them.
113 void DiscardBufferHeadBytes(size_t count, bool do_hash);
114
Darin Petkov0406e402010-10-06 21:33:11 -0700115 // Checkpoints the update progress into persistent storage to allow this
116 // update attempt to be resumed after reboot.
Darin Petkov73058b42010-10-06 16:32:19 -0700117 bool CheckpointUpdateProgress();
118
119 // Update Engine preference store.
120 PrefsInterface* prefs_;
121
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700122 // File descriptor of open device.
123 int fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700124
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700125 // File descriptor of the kernel device
126 int kernel_fd_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700127
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700128 std::string path_; // Path that fd_ refers to.
129 std::string kernel_path_; // Path that kernel_fd_ refers to.
Darin Petkovd7061ab2010-10-06 14:37:09 -0700130
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700131 DeltaArchiveManifest manifest_;
132 bool manifest_valid_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700133
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700134 // Index of the next operation to perform in the manifest.
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700135 int next_operation_num_;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700136
137 // buffer_ is a window of the data that's been downloaded. At first,
138 // it contains the beginning of the download, but after the protobuf
139 // has been downloaded and parsed, it contains a sliding window of
140 // data blobs.
141 std::vector<char> buffer_;
142 // Offset of buffer_ in the binary blobs section of the update.
143 uint64_t buffer_offset_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700144
Darin Petkov0406e402010-10-06 21:33:11 -0700145 // Last |buffer_offset_| value updated as part of the progress update.
146 uint64_t last_updated_buffer_offset_;
147
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700148 // The block size (parsed from the manifest).
149 uint32_t block_size_;
Darin Petkovd7061ab2010-10-06 14:37:09 -0700150
151 // Calculate the payload hash to verify against the signed hash.
152 OmahaHashCalculator hash_calculator_;
153
154 // Signatures message blob extracted directly from the payload.
155 std::vector<char> signatures_message_data_;
156
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700157 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer);
158};
159
160} // namespace chromeos_update_engine
161
162#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__