OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |
6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |
7 | 7 |
8 #include <inttypes.h> | 8 #include <inttypes.h> |
| 9 |
9 #include <vector> | 10 #include <vector> |
| 11 |
10 #include <google/protobuf/repeated_field.h> | 12 #include <google/protobuf/repeated_field.h> |
| 13 |
11 #include "update_engine/file_writer.h" | 14 #include "update_engine/file_writer.h" |
| 15 #include "update_engine/omaha_hash_calculator.h" |
12 #include "update_engine/update_metadata.pb.h" | 16 #include "update_engine/update_metadata.pb.h" |
13 | 17 |
14 namespace chromeos_update_engine { | 18 namespace chromeos_update_engine { |
15 | 19 |
16 // This class performs the actions in a delta update synchronously. The delta | 20 // This class performs the actions in a delta update synchronously. The delta |
17 // update itself should be passed in in chunks as it is received. | 21 // update itself should be passed in in chunks as it is received. |
18 | 22 |
19 class DeltaPerformer : public FileWriter { | 23 class DeltaPerformer : public FileWriter { |
20 public: | 24 public: |
21 DeltaPerformer() | 25 DeltaPerformer() |
22 : fd_(-1), | 26 : fd_(-1), |
23 kernel_fd_(-1), | 27 kernel_fd_(-1), |
24 manifest_valid_(false), | 28 manifest_valid_(false), |
25 next_operation_num_(0), | 29 next_operation_num_(0), |
26 buffer_offset_(0), | 30 buffer_offset_(0), |
27 block_size_(0) {} | 31 block_size_(0) {} |
28 | 32 |
29 // Opens the kernel. Should be called before or after Open(), but before | 33 // Opens the kernel. Should be called before or after Open(), but before |
30 // Write(). The kernel file will be close()d when Close() is called. | 34 // Write(). The kernel file will be close()d when Close() is called. |
31 bool OpenKernel(const char* kernel_path); | 35 bool OpenKernel(const char* kernel_path); |
32 | 36 |
33 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be | 37 // flags and mode ignored. Once Close()d, a DeltaPerformer can't be |
34 // Open()ed again. | 38 // Open()ed again. |
35 int Open(const char* path, int flags, mode_t mode); | 39 int Open(const char* path, int flags, mode_t mode); |
36 | 40 |
37 // Wrapper around write. Returns bytes written on success or | 41 // Wrapper around write. Returns bytes written on success or |
38 // -errno on error. | 42 // -errno on error. |
39 ssize_t Write(const void* bytes, size_t count); | 43 ssize_t Write(const void* bytes, size_t count); |
40 | 44 |
41 // Wrapper around close. Returns 0 on success or -errno on error. | 45 // Wrapper around close. Returns 0 on success or -errno on error. |
42 // Closes both 'path' given to Open() and the kernel path. | 46 // Closes both 'path' given to Open() and the kernel path. |
43 int Close(); | 47 int Close(); |
44 | 48 |
| 49 // Verifies the downloaded payload against the signed hash included in the |
| 50 // payload and returns true on success, false on failure. This method should |
| 51 // be called after closing the stream. Note this method returns true if the |
| 52 // public key is unavailable; it returns false if the public key is available |
| 53 // but the delta payload doesn't include a signature. If |public_key_path| is |
| 54 // an empty string, uses the default public key path. |
| 55 bool VerifyPayload(const std::string& public_key_path); |
| 56 |
45 // Converts an ordered collection of Extent objects which contain data of | 57 // Converts an ordered collection of Extent objects which contain data of |
46 // length full_length to a comma-separated string. For each Extent, the | 58 // length full_length to a comma-separated string. For each Extent, the |
47 // string will have the start offset and then the length in bytes. | 59 // string will have the start offset and then the length in bytes. |
48 // The length value of the last extent in the string may be short, since | 60 // The length value of the last extent in the string may be short, since |
49 // the full length of all extents in the string is capped to full_length. | 61 // the full length of all extents in the string is capped to full_length. |
50 // Also, an extent starting at kSparseHole, appears as -1 in the string. | 62 // Also, an extent starting at kSparseHole, appears as -1 in the string. |
51 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1}, | 63 // For example, if the Extents are {1, 1}, {4, 2}, {kSparseHole, 1}, |
52 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13, | 64 // {0, 1}, block_size is 4096, and full_length is 5 * block_size - 13, |
53 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083" | 65 // the resulting string will be: "4096:4096,16384:8192,-1:4096,0:4083" |
54 static bool ExtentsToBsdiffPositionsString( | 66 static bool ExtentsToBsdiffPositionsString( |
55 const google::protobuf::RepeatedPtrField<Extent>& extents, | 67 const google::protobuf::RepeatedPtrField<Extent>& extents, |
56 uint64_t block_size, | 68 uint64_t block_size, |
57 uint64_t full_length, | 69 uint64_t full_length, |
58 std::string* positions_string); | 70 std::string* positions_string); |
59 | 71 |
60 private: | 72 private: |
61 // Returns true if enough of the delta file has been passed via Write() | 73 // Returns true if enough of the delta file has been passed via Write() |
62 // to be able to perform a given install operation. | 74 // to be able to perform a given install operation. |
63 bool CanPerformInstallOperation( | 75 bool CanPerformInstallOperation( |
64 const DeltaArchiveManifest_InstallOperation& operation); | 76 const DeltaArchiveManifest_InstallOperation& operation); |
65 | 77 |
66 // Returns true on success. | 78 // Returns true on success. |
67 bool PerformInstallOperation( | 79 bool PerformInstallOperation( |
68 const DeltaArchiveManifest_InstallOperation& operation); | 80 const DeltaArchiveManifest_InstallOperation& operation); |
69 | 81 |
70 // These perform a specific type of operation and return true on success. | 82 // These perform a specific type of operation and return true on success. |
71 bool PerformReplaceOperation( | 83 bool PerformReplaceOperation( |
72 const DeltaArchiveManifest_InstallOperation& operation, | 84 const DeltaArchiveManifest_InstallOperation& operation, |
73 bool is_kernel_partition); | 85 bool is_kernel_partition); |
74 bool PerformMoveOperation( | 86 bool PerformMoveOperation( |
75 const DeltaArchiveManifest_InstallOperation& operation, | 87 const DeltaArchiveManifest_InstallOperation& operation, |
76 bool is_kernel_partition); | 88 bool is_kernel_partition); |
77 bool PerformBsdiffOperation( | 89 bool PerformBsdiffOperation( |
78 const DeltaArchiveManifest_InstallOperation& operation, | 90 const DeltaArchiveManifest_InstallOperation& operation, |
79 bool is_kernel_partition); | 91 bool is_kernel_partition); |
80 | 92 |
| 93 // Returns true if the payload signature message has been extracted from |
| 94 // |operation|, false otherwise. |
| 95 bool ExtractSignatureMessage( |
| 96 const DeltaArchiveManifest_InstallOperation& operation); |
| 97 |
| 98 // Discard |count| bytes from the beginning of buffer_. If |do_hash| is true, |
| 99 // updates the hash calculator with these bytes before discarding them. |
| 100 void DiscardBufferHeadBytes(size_t count, bool do_hash); |
| 101 |
81 // File descriptor of open device. | 102 // File descriptor of open device. |
82 int fd_; | 103 int fd_; |
83 | 104 |
84 // File descriptor of the kernel device | 105 // File descriptor of the kernel device |
85 int kernel_fd_; | 106 int kernel_fd_; |
86 | 107 |
87 std::string path_; // Path that fd_ refers to. | 108 std::string path_; // Path that fd_ refers to. |
88 std::string kernel_path_; // Path that kernel_fd_ refers to. | 109 std::string kernel_path_; // Path that kernel_fd_ refers to. |
89 | 110 |
90 DeltaArchiveManifest manifest_; | 111 DeltaArchiveManifest manifest_; |
91 bool manifest_valid_; | 112 bool manifest_valid_; |
92 | 113 |
93 // Index of the next operation to perform in the manifest. | 114 // Index of the next operation to perform in the manifest. |
94 int next_operation_num_; | 115 int next_operation_num_; |
95 | 116 |
96 // buffer_ is a window of the data that's been downloaded. At first, | 117 // buffer_ is a window of the data that's been downloaded. At first, |
97 // it contains the beginning of the download, but after the protobuf | 118 // it contains the beginning of the download, but after the protobuf |
98 // has been downloaded and parsed, it contains a sliding window of | 119 // has been downloaded and parsed, it contains a sliding window of |
99 // data blobs. | 120 // data blobs. |
100 std::vector<char> buffer_; | 121 std::vector<char> buffer_; |
101 // Offset of buffer_ in the binary blobs section of the update. | 122 // Offset of buffer_ in the binary blobs section of the update. |
102 uint64_t buffer_offset_; | 123 uint64_t buffer_offset_; |
103 | 124 |
104 // The block size (parsed from the manifest). | 125 // The block size (parsed from the manifest). |
105 uint32_t block_size_; | 126 uint32_t block_size_; |
106 | 127 |
| 128 // Calculate the payload hash to verify against the signed hash. |
| 129 OmahaHashCalculator hash_calculator_; |
| 130 |
| 131 // Signatures message blob extracted directly from the payload. |
| 132 std::vector<char> signatures_message_data_; |
| 133 |
107 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); | 134 DISALLOW_COPY_AND_ASSIGN(DeltaPerformer); |
108 }; | 135 }; |
109 | 136 |
110 } // namespace chromeos_update_engine | 137 } // namespace chromeos_update_engine |
111 | 138 |
112 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ | 139 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_PERFORMER_H__ |
OLD | NEW |