blob: 150b0b019ce7b36aea8cbc5881b6780c56caa2ed [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.
53 static bool AddOperationHash(DeltaArchiveManifest_InstallOperation* op,
54 const chromeos::Blob& buf);
55
56 // Install operations in the manifest may reference data blobs, which
57 // are in data_blobs_path. This function creates a new data blobs file
58 // with the data blobs in the same order as the referencing install
59 // operations in the manifest. E.g. if manifest[0] has a data blob
60 // "X" at offset 1, manifest[1] has a data blob "Y" at offset 0,
61 // and data_blobs_path's file contains "YX", new_data_blobs_path
62 // will set to be a file that contains "XY".
63 bool ReorderDataBlobs(const std::string& data_blobs_path,
64 const std::string& new_data_blobs_path);
65
66 // Print in stderr the Payload usage report.
67 void ReportPayloadUsage(uint64_t metadata_size) const;
68
69 // The list of partitions in the order their blobs should appear in the
70 // payload file.
71 static const std::vector<PartitionName> partition_disk_order_;
72
73 DeltaArchiveManifest manifest_;
74
75 std::map<PartitionName, std::vector<AnnotatedOperation>> aops_map_;
76};
77
78// Adds a dummy operation that points to a signature blob located at the
79// specified offset/length.
80void AddSignatureOp(uint64_t signature_blob_offset,
81 uint64_t signature_blob_length,
82 DeltaArchiveManifest* manifest);
83
84} // namespace chromeos_update_engine
85
86#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_PAYLOAD_FILE_H_