blob: 8b1795698b5b735627e4bc9819f77641efed2255 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo14158572015-06-13 03:37:08 -070016
17#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_
19
Alex Deymo14158572015-06-13 03:37:08 -070020#include <string>
21#include <vector>
22
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070023#include <brillo/secure_blob.h>
Alex Deymo14158572015-06-13 03:37:08 -070024#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
30namespace 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.
35class 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 Jiangb9ef4912015-09-21 15:06:13 -070041 // Add a partition to the payload manifest. Including partition name, list of
42 // operations and partition info. The operations in |aops|
Alex Deymo14158572015-06-13 03:37:08 -070043 // reference a blob stored in the file provided to WritePayload().
Sen Jiangb9ef4912015-09-21 15:06:13 -070044 bool AddPartition(const PartitionConfig& old_conf,
45 const PartitionConfig& new_conf,
Tianjiee9156ec2020-08-11 11:13:54 -070046 std::vector<AnnotatedOperation> aops,
47 std::vector<CowMergeOperation> merge_sequence);
Alex Deymo14158572015-06-13 03:37:08 -070048
49 // Write the payload to the |payload_file| file. The operations reference
50 // blobs in the |data_blobs_path| file and the blobs will be reordered in the
51 // payload file to match the order of the operations. The size of the metadata
52 // section of the payload is stored in |metadata_size_out|.
53 bool WritePayload(const std::string& payload_file,
54 const std::string& data_blobs_path,
55 const std::string& private_key_path,
Sen Jiangaef1c6f2015-10-07 10:05:32 -070056 uint64_t* metadata_size_out);
Alex Deymo14158572015-06-13 03:37:08 -070057
58 private:
59 FRIEND_TEST(PayloadFileTest, ReorderBlobsTest);
60
61 // Computes a SHA256 hash of the given buf and sets the hash value in the
62 // operation so that update_engine could verify. This hash should be set
63 // for all operations that have a non-zero data blob. One exception is the
Tianjied60dc392020-07-29 11:27:35 -070064 // fake operation for signature blob because the contents of the signature
Alex Deymo14158572015-06-13 03:37:08 -070065 // blob will not be available at payload creation time. So, update_engine will
Tianjied60dc392020-07-29 11:27:35 -070066 // gracefully ignore the fake signature operation.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070067 static bool AddOperationHash(InstallOperation* op, const brillo::Blob& buf);
Alex Deymo14158572015-06-13 03:37:08 -070068
69 // Install operations in the manifest may reference data blobs, which
70 // are in data_blobs_path. This function creates a new data blobs file
71 // with the data blobs in the same order as the referencing install
72 // operations in the manifest. E.g. if manifest[0] has a data blob
73 // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0,
74 // and data_blobs_path's file contains "YX", new_data_blobs_path
75 // will set to be a file that contains "XY".
76 bool ReorderDataBlobs(const std::string& data_blobs_path,
77 const std::string& new_data_blobs_path);
78
79 // Print in stderr the Payload usage report.
80 void ReportPayloadUsage(uint64_t metadata_size) const;
81
Sen Jiang46e9b172015-08-31 14:11:01 -070082 // The major_version of the requested payload.
83 uint64_t major_version_;
84
Alex Deymo14158572015-06-13 03:37:08 -070085 DeltaArchiveManifest manifest_;
86
Sen Jiangb9ef4912015-09-21 15:06:13 -070087 // Struct has necessary information to write PartitionUpdate in protobuf.
88 struct Partition {
89 // The name of the partition.
90 std::string name;
91
92 // The operations to be performed to this partition.
93 std::vector<AnnotatedOperation> aops;
Tianjiee9156ec2020-08-11 11:13:54 -070094 std::vector<CowMergeOperation> cow_merge_sequence;
Sen Jiangb9ef4912015-09-21 15:06:13 -070095
96 PartitionInfo old_info;
97 PartitionInfo new_info;
Sen Jiang05feee02015-11-11 15:59:49 -080098
99 PostInstallConfig postinstall;
Sen Jiang3a4dfac2018-08-30 16:57:38 -0700100 VerityConfig verity;
Kelvin Zhang1f496422020-08-11 17:18:23 -0400101 // Per partition timestamp.
102 std::string version;
Sen Jiangb9ef4912015-09-21 15:06:13 -0700103 };
104
105 std::vector<Partition> part_vec_;
Alex Deymo14158572015-06-13 03:37:08 -0700106};
107
Alex Deymo14158572015-06-13 03:37:08 -0700108} // namespace chromeos_update_engine
109
110#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_