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