Don Garrett | f4b2874 | 2012-03-27 20:48:06 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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_DIFF_GENERATOR_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_DIFF_GENERATOR_H__ |
| 7 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 10 | #include "base/basictypes.h" |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 11 | #include "update_engine/graph_types.h" |
| 12 | #include "update_engine/update_metadata.pb.h" |
| 13 | |
| 14 | // There is one function in DeltaDiffGenerator of importance to users |
| 15 | // of the class: GenerateDeltaUpdateFile(). Before calling it, |
| 16 | // the old and new images must be mounted. Call GenerateDeltaUpdateFile() |
| 17 | // with both the mount-points of the images in addition to the paths of |
| 18 | // the images (both old and new). A delta from old to new will be |
| 19 | // generated and stored in output_path. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 23 | // This struct stores all relevant info for an edge that is cut between |
| 24 | // nodes old_src -> old_dst by creating new vertex new_vertex. The new |
| 25 | // relationship is: |
| 26 | // old_src -(read before)-> new_vertex <-(write before)- old_dst |
| 27 | // new_vertex is a MOVE operation that moves some existing blocks into |
| 28 | // temp space. The temp extents are, by necessity, stored in new_vertex |
| 29 | // (as dst extents) and old_dst (as src extents), but they are also broken |
| 30 | // out into tmp_extents, as the nodes themselves may contain many more |
| 31 | // extents. |
| 32 | struct CutEdgeVertexes { |
| 33 | Vertex::Index new_vertex; |
| 34 | Vertex::Index old_src; |
| 35 | Vertex::Index old_dst; |
| 36 | std::vector<Extent> tmp_extents; |
| 37 | }; |
| 38 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 39 | class DeltaDiffGenerator { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 40 | public: |
| 41 | // Represents a disk block on the install partition. |
| 42 | struct Block { |
| 43 | // During install, each block on the install partition will be written |
| 44 | // and some may be read (in all likelihood, many will be read). |
| 45 | // The reading and writing will be performed by InstallOperations, |
| 46 | // each of which has a corresponding vertex in a graph. |
| 47 | // A Block object tells which vertex will read or write this block |
| 48 | // at install time. |
| 49 | // Generally, there will be a vector of Block objects whose length |
| 50 | // is the number of blocks on the install partition. |
| 51 | Block() : reader(Vertex::kInvalidIndex), writer(Vertex::kInvalidIndex) {} |
| 52 | Vertex::Index reader; |
| 53 | Vertex::Index writer; |
| 54 | }; |
| 55 | |
| 56 | // This is the only function that external users of the class should call. |
| 57 | // old_image and new_image are paths to two image files. They should be |
| 58 | // mounted read-only at paths old_root and new_root respectively. |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 59 | // {old,new}_kernel_part are paths to the old and new kernel partition |
| 60 | // images, respectively. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 61 | // private_key_path points to a private key used to sign the update. |
| 62 | // Pass empty string to not sign the update. |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 63 | // output_path is the filename where the delta update should be written. |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 64 | // Returns true on success. Also writes the size of the metadata into |
| 65 | // |metadata_size|. |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 66 | static bool GenerateDeltaUpdateFile(const std::string& old_root, |
| 67 | const std::string& old_image, |
| 68 | const std::string& new_root, |
| 69 | const std::string& new_image, |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 70 | const std::string& old_kernel_part, |
| 71 | const std::string& new_kernel_part, |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 72 | const std::string& output_path, |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 73 | const std::string& private_key_path, |
| 74 | uint64_t* metadata_size); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 75 | |
| 76 | // These functions are public so that the unit tests can access them: |
| 77 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 78 | // Takes a graph, which is not a DAG, which represents the files just |
| 79 | // read from disk, and converts it into a DAG by breaking all cycles |
| 80 | // and finding temp space to resolve broken edges. |
| 81 | // The final order of the nodes is given in |final_order| |
| 82 | // Some files may need to be reread from disk, thus |fd| and |
| 83 | // |data_file_size| are be passed. |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 84 | // If |scratch_vertex| is not kInvalidIndex, removes it from |
| 85 | // |final_order| before returning. |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 86 | // Returns true on success. |
| 87 | static bool ConvertGraphToDag(Graph* graph, |
| 88 | const std::string& new_root, |
| 89 | int fd, |
| 90 | off_t* data_file_size, |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 91 | std::vector<Vertex::Index>* final_order, |
| 92 | Vertex::Index scratch_vertex); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 93 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 94 | // Reads old_filename (if it exists) and a new_filename and determines |
| 95 | // the smallest way to encode this file for the diff. It stores |
| 96 | // necessary data in out_data and fills in out_op. |
| 97 | // If there's no change in old and new files, it creates a MOVE |
| 98 | // operation. If there is a change, or the old file doesn't exist, |
| 99 | // the smallest of REPLACE, REPLACE_BZ, or BSDIFF wins. |
| 100 | // new_filename must contain at least one byte. |
| 101 | // Returns true on success. |
| 102 | static bool ReadFileToDiff(const std::string& old_filename, |
| 103 | const std::string& new_filename, |
Don Garrett | 36e6077 | 2012-03-29 10:31:20 -0700 | [diff] [blame] | 104 | bool bsdiff_allowed, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 105 | std::vector<char>* out_data, |
Darin Petkov | 68c10d1 | 2010-10-14 09:24:37 -0700 | [diff] [blame] | 106 | DeltaArchiveManifest_InstallOperation* out_op, |
| 107 | bool gather_extents); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 108 | |
Andrew de los Reyes | 927179d | 2010-12-02 11:26:48 -0800 | [diff] [blame] | 109 | // Creates a dummy REPLACE_BZ node in the given |vertex|. This can be used |
| 110 | // to provide scratch space. The node writes |num_blocks| blocks starting at |
| 111 | // |start_block|The node should be marked invalid before writing all nodes to |
| 112 | // the output file. |
| 113 | static void CreateScratchNode(uint64_t start_block, |
| 114 | uint64_t num_blocks, |
| 115 | Vertex* vertex); |
| 116 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 117 | // Modifies blocks read by 'op' so that any blocks referred to by |
| 118 | // 'remove_extents' are replaced with blocks from 'replace_extents'. |
| 119 | // 'remove_extents' and 'replace_extents' must be the same number of blocks. |
| 120 | // Blocks will be substituted in the order listed in the vectors. |
| 121 | // E.g. if 'op' reads blocks 1, 2, 3, 4, 5, 6, 7, 8, remove_extents |
| 122 | // contains blocks 6, 2, 3, 5, and replace blocks contains |
| 123 | // 12, 13, 14, 15, then op will be changed to read from: |
| 124 | // 1, 13, 14, 4, 15, 12, 7, 8 |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 125 | static void SubstituteBlocks(Vertex* vertex, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 126 | const std::vector<Extent>& remove_extents, |
| 127 | const std::vector<Extent>& replace_extents); |
| 128 | |
| 129 | // Cuts 'edges' from 'graph' according to the AU algorithm. This means |
| 130 | // for each edge A->B, remove the dependency that B occur before A. |
| 131 | // Do this by creating a new operation X that copies from the blocks |
| 132 | // specified by the edge's properties to temp space T. Modify B to read |
| 133 | // from T rather than the blocks in the edge. Modify A to depend on X, |
| 134 | // but not on B. Free space is found by looking in 'blocks'. |
| 135 | // Returns true on success. |
| 136 | static bool CutEdges(Graph* graph, |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 137 | const std::set<Edge>& edges, |
| 138 | std::vector<CutEdgeVertexes>* out_cuts); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 139 | |
| 140 | // Stores all Extents in 'extents' into 'out'. |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 141 | static void StoreExtents(const std::vector<Extent>& extents, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 142 | google::protobuf::RepeatedPtrField<Extent>* out); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 143 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 144 | // Creates all the edges for the graph. Writers of a block point to |
| 145 | // readers of the same block. This is because for an edge A->B, B |
| 146 | // must complete before A executes. |
| 147 | static void CreateEdges(Graph* graph, const std::vector<Block>& blocks); |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 148 | |
| 149 | // Given a topologically sorted graph |op_indexes| and |graph|, alters |
| 150 | // |op_indexes| to move all the full operations to the end of the vector. |
| 151 | // Full operations should not be depended on, so this is safe. |
| 152 | static void MoveFullOpsToBack(Graph* graph, |
| 153 | std::vector<Vertex::Index>* op_indexes); |
| 154 | |
| 155 | // Sorts the vector |cuts| by its |cuts[].old_dest| member. Order is |
| 156 | // determined by the order of elements in op_indexes. |
| 157 | static void SortCutsByTopoOrder(std::vector<Vertex::Index>& op_indexes, |
| 158 | std::vector<CutEdgeVertexes>* cuts); |
| 159 | |
| 160 | // Returns true iff there are no extents in the graph that refer to temp |
| 161 | // blocks. Temp blocks are in the range [kTempBlockStart, kSparseHole). |
| 162 | static bool NoTempBlocksRemain(const Graph& graph); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 163 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 164 | // Install operations in the manifest may reference data blobs, which |
| 165 | // are in data_blobs_path. This function creates a new data blobs file |
| 166 | // with the data blobs in the same order as the referencing install |
| 167 | // operations in the manifest. E.g. if manifest[0] has a data blob |
| 168 | // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0, |
| 169 | // and data_blobs_path's file contains "YX", new_data_blobs_path |
| 170 | // will set to be a file that contains "XY". |
| 171 | static bool ReorderDataBlobs(DeltaArchiveManifest* manifest, |
| 172 | const std::string& data_blobs_path, |
| 173 | const std::string& new_data_blobs_path); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 174 | |
Jay Srinivasan | 00f76b6 | 2012-09-17 18:48:36 -0700 | [diff] [blame] | 175 | // Computes a SHA256 hash of the given buf and sets the hash value in the |
| 176 | // operation so that update_engine could verify. This hash should be set |
| 177 | // for all operations that have a non-zero data blob. One exception is the |
| 178 | // dummy operation for signature blob because the contents of the signature |
| 179 | // blob will not be available at payload creation time. So, update_engine will |
| 180 | // gracefully ignore the dummy signature operation. |
| 181 | static bool AddOperationHash(DeltaArchiveManifest_InstallOperation* op, |
| 182 | const std::vector<char>& buf); |
| 183 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 184 | // Handles allocation of temp blocks to a cut edge by converting the |
| 185 | // dest node to a full op. This removes the need for temp blocks, but |
| 186 | // comes at the cost of a worse compression ratio. |
| 187 | // For example, say we have A->B->A. It would first be cut to form: |
| 188 | // A->B->N<-A, where N copies blocks to temp space. If there are no |
| 189 | // temp blocks, this function can be called to convert it to the form: |
| 190 | // A->B. Now, A is a full operation. |
| 191 | static bool ConvertCutToFullOp(Graph* graph, |
| 192 | const CutEdgeVertexes& cut, |
| 193 | const std::string& new_root, |
| 194 | int data_fd, |
| 195 | off_t* data_file_size); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 196 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 197 | // Takes |op_indexes|, which is effectively a mapping from order in |
| 198 | // which the op is performed -> graph vertex index, and produces the |
| 199 | // reverse: a mapping from graph vertex index -> op_indexes index. |
| 200 | static void GenerateReverseTopoOrderMap( |
| 201 | std::vector<Vertex::Index>& op_indexes, |
| 202 | std::vector<std::vector<Vertex::Index>::size_type>* reverse_op_indexes); |
Darin Petkov | 7ea3233 | 2010-10-13 10:46:11 -0700 | [diff] [blame] | 203 | |
Andrew de los Reyes | ef01755 | 2010-10-06 17:57:52 -0700 | [diff] [blame] | 204 | // Takes a |graph|, which has edges that must be cut, as listed in |
| 205 | // |cuts|. Cuts the edges. Maintains a list in which the operations |
| 206 | // will be performed (in |op_indexes|) and the reverse (in |
| 207 | // |reverse_op_indexes|). Cutting edges requires scratch space, and |
| 208 | // if insufficient scratch is found, the file is reread and will be |
| 209 | // send down (either as REPLACE or REPLACE_BZ). Returns true on |
| 210 | // success. |
| 211 | static bool AssignTempBlocks( |
| 212 | Graph* graph, |
| 213 | const std::string& new_root, |
| 214 | int data_fd, |
| 215 | off_t* data_file_size, |
| 216 | std::vector<Vertex::Index>* op_indexes, |
| 217 | std::vector<std::vector<Vertex::Index>::size_type>* reverse_op_indexes, |
Andrew de los Reyes | 4ba850d | 2010-10-25 12:12:40 -0700 | [diff] [blame] | 218 | const std::vector<CutEdgeVertexes>& cuts); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 219 | |
Darin Petkov | 9fa7ec5 | 2010-10-18 11:45:23 -0700 | [diff] [blame] | 220 | // Returns true if |op| is a no-op operation that doesn't do any useful work |
| 221 | // (e.g., a move operation that copies blocks onto themselves). |
| 222 | static bool IsNoopOperation(const DeltaArchiveManifest_InstallOperation& op); |
| 223 | |
Andrew de los Reyes | 89f17be | 2010-10-22 13:39:09 -0700 | [diff] [blame] | 224 | static bool InitializePartitionInfo(bool is_kernel, |
| 225 | const std::string& partition, |
| 226 | PartitionInfo* info); |
| 227 | |
Thieu Le | 5c7d975 | 2010-12-15 16:09:28 -0800 | [diff] [blame] | 228 | // Runs the bsdiff tool on two files and returns the resulting delta in |
| 229 | // |out|. Returns true on success. |
| 230 | static bool BsdiffFiles(const std::string& old_file, |
| 231 | const std::string& new_file, |
| 232 | std::vector<char>* out); |
| 233 | |
| 234 | // The |blocks| vector contains a reader and writer for each block on the |
| 235 | // filesystem that's being in-place updated. We populate the reader/writer |
| 236 | // fields of |blocks| by calling this function. |
| 237 | // For each block in |operation| that is read or written, find that block |
| 238 | // in |blocks| and set the reader/writer field to the vertex passed. |
| 239 | // |graph| is not strictly necessary, but useful for printing out |
| 240 | // error messages. |
| 241 | static bool AddInstallOpToBlocksVector( |
| 242 | const DeltaArchiveManifest_InstallOperation& operation, |
| 243 | const Graph& graph, |
| 244 | Vertex::Index vertex, |
| 245 | std::vector<DeltaDiffGenerator::Block>* blocks); |
| 246 | |
Darin Petkov | 9574f7e | 2011-01-13 10:48:12 -0800 | [diff] [blame] | 247 | // Adds to |manifest| a dummy operation that points to a signature blob |
| 248 | // located at the specified offset/length. |
| 249 | static void AddSignatureOp(uint64_t signature_blob_offset, |
| 250 | uint64_t signature_blob_length, |
| 251 | DeltaArchiveManifest* manifest); |
| 252 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 253 | private: |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 254 | // This should never be constructed |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 255 | DISALLOW_IMPLICIT_CONSTRUCTORS(DeltaDiffGenerator); |
| 256 | }; |
| 257 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 258 | extern const char* const kBsdiffPath; |
| 259 | extern const char* const kBspatchPath; |
| 260 | extern const char* const kDeltaMagic; |
| 261 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 262 | }; // namespace chromeos_update_engine |
| 263 | |
| 264 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DELTA_DIFF_GENERATOR_H__ |