Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_ |
| 18 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_ |
| 19 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <chromeos/secure_blob.h> |
| 24 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 25 | |
| 26 | #include "update_engine/payload_generator/annotated_operation.h" |
| 27 | #include "update_engine/payload_generator/payload_generation_config.h" |
| 28 | #include "update_engine/update_metadata.pb.h" |
| 29 | |
| 30 | namespace chromeos_update_engine { |
| 31 | |
| 32 | // Class to handle the creation of a payload file. This class is the only one |
| 33 | // dealing with writing the payload and its format, but has no logic about what |
| 34 | // should be on it. |
| 35 | class PayloadFile { |
| 36 | public: |
| 37 | // Initialize the payload file with the payload generation config. It computes |
| 38 | // required hashes of the requested partitions. |
| 39 | bool Init(const PayloadGenerationConfig& config); |
| 40 | |
Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame^] | 41 | // Add a partition to the payload manifest. Including partition name, list of |
| 42 | // operations and partition info. The operations in |aops| |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 43 | // reference a blob stored in the file provided to WritePayload(). |
Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame^] | 44 | bool AddPartition(const PartitionConfig& old_conf, |
| 45 | const PartitionConfig& new_conf, |
| 46 | const std::vector<AnnotatedOperation>& aops); |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 47 | |
| 48 | // Write the payload to the |payload_file| file. The operations reference |
| 49 | // blobs in the |data_blobs_path| file and the blobs will be reordered in the |
| 50 | // payload file to match the order of the operations. The size of the metadata |
| 51 | // section of the payload is stored in |metadata_size_out|. |
| 52 | bool WritePayload(const std::string& payload_file, |
| 53 | const std::string& data_blobs_path, |
| 54 | const std::string& private_key_path, |
| 55 | uint64_t* medatata_size_out); |
| 56 | |
| 57 | private: |
| 58 | FRIEND_TEST(PayloadFileTest, ReorderBlobsTest); |
| 59 | |
| 60 | // Computes a SHA256 hash of the given buf and sets the hash value in the |
| 61 | // operation so that update_engine could verify. This hash should be set |
| 62 | // for all operations that have a non-zero data blob. One exception is the |
| 63 | // dummy operation for signature blob because the contents of the signature |
| 64 | // blob will not be available at payload creation time. So, update_engine will |
| 65 | // gracefully ignore the dummy signature operation. |
Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 66 | static bool AddOperationHash(InstallOperation* op, const chromeos::Blob& buf); |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 67 | |
| 68 | // Install operations in the manifest may reference data blobs, which |
| 69 | // are in data_blobs_path. This function creates a new data blobs file |
| 70 | // with the data blobs in the same order as the referencing install |
| 71 | // operations in the manifest. E.g. if manifest[0] has a data blob |
| 72 | // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0, |
| 73 | // and data_blobs_path's file contains "YX", new_data_blobs_path |
| 74 | // will set to be a file that contains "XY". |
| 75 | bool ReorderDataBlobs(const std::string& data_blobs_path, |
| 76 | const std::string& new_data_blobs_path); |
| 77 | |
| 78 | // Print in stderr the Payload usage report. |
| 79 | void ReportPayloadUsage(uint64_t metadata_size) const; |
| 80 | |
Sen Jiang | 46e9b17 | 2015-08-31 14:11:01 -0700 | [diff] [blame] | 81 | // The major_version of the requested payload. |
| 82 | uint64_t major_version_; |
| 83 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 84 | DeltaArchiveManifest manifest_; |
| 85 | |
Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame^] | 86 | // Struct has necessary information to write PartitionUpdate in protobuf. |
| 87 | struct Partition { |
| 88 | // The name of the partition. |
| 89 | std::string name; |
| 90 | |
| 91 | // The operations to be performed to this partition. |
| 92 | std::vector<AnnotatedOperation> aops; |
| 93 | |
| 94 | PartitionInfo old_info; |
| 95 | PartitionInfo new_info; |
| 96 | }; |
| 97 | |
| 98 | std::vector<Partition> part_vec_; |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | // Adds a dummy operation that points to a signature blob located at the |
| 102 | // specified offset/length. |
| 103 | void AddSignatureOp(uint64_t signature_blob_offset, |
| 104 | uint64_t signature_blob_length, |
| 105 | DeltaArchiveManifest* manifest); |
| 106 | |
| 107 | } // namespace chromeos_update_engine |
| 108 | |
| 109 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_ |