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 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 23 | #include <brillo/secure_blob.h> |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 24 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 25 | |
Daniel Zheng | 09d022d | 2023-11-13 15:30:03 -0800 | [diff] [blame^] | 26 | #include <libsnapshot/cow_writer.h> |
| 27 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 28 | #include "update_engine/payload_generator/annotated_operation.h" |
| 29 | #include "update_engine/payload_generator/payload_generation_config.h" |
| 30 | #include "update_engine/update_metadata.pb.h" |
| 31 | |
| 32 | namespace chromeos_update_engine { |
| 33 | |
| 34 | // Class to handle the creation of a payload file. This class is the only one |
| 35 | // dealing with writing the payload and its format, but has no logic about what |
| 36 | // should be on it. |
| 37 | class PayloadFile { |
| 38 | public: |
| 39 | // Initialize the payload file with the payload generation config. It computes |
| 40 | // required hashes of the requested partitions. |
| 41 | bool Init(const PayloadGenerationConfig& config); |
| 42 | |
Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 43 | // Add a partition to the payload manifest. Including partition name, list of |
| 44 | // operations and partition info. The operations in |aops| |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 45 | // reference a blob stored in the file provided to WritePayload(). |
Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 46 | bool AddPartition(const PartitionConfig& old_conf, |
| 47 | const PartitionConfig& new_conf, |
Tianjie | e9156ec | 2020-08-11 11:13:54 -0700 | [diff] [blame] | 48 | std::vector<AnnotatedOperation> aops, |
Kelvin Zhang | 7a26575 | 2020-10-29 15:51:35 -0400 | [diff] [blame] | 49 | std::vector<CowMergeOperation> merge_sequence, |
Daniel Zheng | 09d022d | 2023-11-13 15:30:03 -0800 | [diff] [blame^] | 50 | const android::snapshot::CowSizeInfo& cow_info); |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 51 | |
| 52 | // Write the payload to the |payload_file| file. The operations reference |
| 53 | // blobs in the |data_blobs_path| file and the blobs will be reordered in the |
| 54 | // payload file to match the order of the operations. The size of the metadata |
| 55 | // section of the payload is stored in |metadata_size_out|. |
| 56 | bool WritePayload(const std::string& payload_file, |
| 57 | const std::string& data_blobs_path, |
| 58 | const std::string& private_key_path, |
Sen Jiang | aef1c6f | 2015-10-07 10:05:32 -0700 | [diff] [blame] | 59 | uint64_t* metadata_size_out); |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 60 | |
Kelvin Zhang | f041a9d | 2021-10-06 18:58:39 -0700 | [diff] [blame] | 61 | static bool WritePayload(const std::string& payload_file, |
| 62 | const std::string& ordered_blobs_file, |
| 63 | const std::string& private_key_path, |
| 64 | uint64_t major_version_, |
| 65 | const DeltaArchiveManifest& manifest, |
| 66 | uint64_t* out_metadata_size); |
| 67 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 68 | private: |
| 69 | FRIEND_TEST(PayloadFileTest, ReorderBlobsTest); |
| 70 | |
| 71 | // Computes a SHA256 hash of the given buf and sets the hash value in the |
| 72 | // operation so that update_engine could verify. This hash should be set |
| 73 | // for all operations that have a non-zero data blob. One exception is the |
Tianjie | d60dc39 | 2020-07-29 11:27:35 -0700 | [diff] [blame] | 74 | // fake operation for signature blob because the contents of the signature |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 75 | // blob will not be available at payload creation time. So, update_engine will |
Tianjie | d60dc39 | 2020-07-29 11:27:35 -0700 | [diff] [blame] | 76 | // gracefully ignore the fake signature operation. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 77 | static bool AddOperationHash(InstallOperation* op, const brillo::Blob& buf); |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 78 | |
| 79 | // Install operations in the manifest may reference data blobs, which |
| 80 | // are in data_blobs_path. This function creates a new data blobs file |
| 81 | // with the data blobs in the same order as the referencing install |
| 82 | // operations in the manifest. E.g. if manifest[0] has a data blob |
| 83 | // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0, |
| 84 | // and data_blobs_path's file contains "YX", new_data_blobs_path |
| 85 | // will set to be a file that contains "XY". |
| 86 | bool ReorderDataBlobs(const std::string& data_blobs_path, |
| 87 | const std::string& new_data_blobs_path); |
| 88 | |
| 89 | // Print in stderr the Payload usage report. |
| 90 | void ReportPayloadUsage(uint64_t metadata_size) const; |
| 91 | |
Sen Jiang | 46e9b17 | 2015-08-31 14:11:01 -0700 | [diff] [blame] | 92 | // The major_version of the requested payload. |
| 93 | uint64_t major_version_; |
| 94 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 95 | DeltaArchiveManifest manifest_; |
| 96 | |
Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 97 | // Struct has necessary information to write PartitionUpdate in protobuf. |
| 98 | struct Partition { |
| 99 | // The name of the partition. |
| 100 | std::string name; |
| 101 | |
| 102 | // The operations to be performed to this partition. |
| 103 | std::vector<AnnotatedOperation> aops; |
Tianjie | e9156ec | 2020-08-11 11:13:54 -0700 | [diff] [blame] | 104 | std::vector<CowMergeOperation> cow_merge_sequence; |
Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 105 | |
| 106 | PartitionInfo old_info; |
| 107 | PartitionInfo new_info; |
Sen Jiang | 05feee0 | 2015-11-11 15:59:49 -0800 | [diff] [blame] | 108 | |
| 109 | PostInstallConfig postinstall; |
Sen Jiang | 3a4dfac | 2018-08-30 16:57:38 -0700 | [diff] [blame] | 110 | VerityConfig verity; |
Kelvin Zhang | 1f49642 | 2020-08-11 17:18:23 -0400 | [diff] [blame] | 111 | // Per partition timestamp. |
| 112 | std::string version; |
Daniel Zheng | 09d022d | 2023-11-13 15:30:03 -0800 | [diff] [blame^] | 113 | android::snapshot::CowSizeInfo cow_info; |
Sen Jiang | b9ef491 | 2015-09-21 15:06:13 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | std::vector<Partition> part_vec_; |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame] | 119 | } // namespace chromeos_update_engine |
| 120 | |
| 121 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_ |