payload_file.h 4.2 KB

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