payload_file.h revision 46e9b1735a16518d563b3c7e7227408f8fc3e891
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//
16
17#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_
19
20#include <map>
21#include <string>
22#include <vector>
23
24#include <chromeos/secure_blob.h>
25#include <gtest/gtest_prod.h>  // for FRIEND_TEST
26
27#include "update_engine/payload_generator/annotated_operation.h"
28#include "update_engine/payload_generator/payload_generation_config.h"
29#include "update_engine/update_metadata.pb.h"
30
31namespace chromeos_update_engine {
32
33// Class to handle the creation of a payload file. This class is the only one
34// dealing with writing the payload and its format, but has no logic about what
35// should be on it.
36class PayloadFile {
37 public:
38  // Initialize the payload file with the payload generation config. It computes
39  // required hashes of the requested partitions.
40  bool Init(const PayloadGenerationConfig& config);
41
42  // Sets the list of operations to the payload manifest. The operations
43  // reference a blob stored in the file provided to WritePayload().
44  void AddPartitionOperations(PartitionName name,
45                              const std::vector<AnnotatedOperation>& aops);
46
47  // Write the payload to the |payload_file| file. The operations reference
48  // blobs in the |data_blobs_path| file and the blobs will be reordered in the
49  // payload file to match the order of the operations. The size of the metadata
50  // section of the payload is stored in |metadata_size_out|.
51  bool WritePayload(const std::string& payload_file,
52                    const std::string& data_blobs_path,
53                    const std::string& private_key_path,
54                    uint64_t* medatata_size_out);
55
56 private:
57  FRIEND_TEST(PayloadFileTest, ReorderBlobsTest);
58
59  // Computes a SHA256 hash of the given buf and sets the hash value in the
60  // operation so that update_engine could verify. This hash should be set
61  // for all operations that have a non-zero data blob. One exception is the
62  // dummy operation for signature blob because the contents of the signature
63  // blob will not be available at payload creation time. So, update_engine will
64  // gracefully ignore the dummy signature operation.
65  static bool AddOperationHash(InstallOperation* op, const chromeos::Blob& buf);
66
67  // Install operations in the manifest may reference data blobs, which
68  // are in data_blobs_path. This function creates a new data blobs file
69  // with the data blobs in the same order as the referencing install
70  // operations in the manifest. E.g. if manifest[0] has a data blob
71  // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0,
72  // and data_blobs_path's file contains "YX", new_data_blobs_path
73  // will set to be a file that contains "XY".
74  bool ReorderDataBlobs(const std::string& data_blobs_path,
75                        const std::string& new_data_blobs_path);
76
77  // Print in stderr the Payload usage report.
78  void ReportPayloadUsage(uint64_t metadata_size) const;
79
80  // The list of partitions in the order their blobs should appear in the
81  // payload file.
82  static const std::vector<PartitionName> partition_disk_order_;
83
84  // The major_version of the requested payload.
85  uint64_t major_version_;
86
87  DeltaArchiveManifest manifest_;
88
89  std::map<PartitionName, std::vector<AnnotatedOperation>> aops_map_;
90};
91
92// Adds a dummy operation that points to a signature blob located at the
93// specified offset/length.
94void AddSignatureOp(uint64_t signature_blob_offset,
95                    uint64_t signature_blob_length,
96                    DeltaArchiveManifest* manifest);
97
98}  // namespace chromeos_update_engine
99
100#endif  // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_
101