1 // 2 // Copyright (C) 2023 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 #pragma once 17 18 #include "common/libs/utils/result.h" 19 20 #include <sys/types.h> 21 22 #include <cstdint> 23 #include <memory> 24 #include <string> 25 #include <vector> 26 27 #include <fruit/fruit.h> 28 29 #include "common/libs/fs/shared_fd.h" 30 #include "common/libs/utils/subprocess.h" 31 32 namespace cuttlefish { 33 34 // Taken from external/avb/avbtool.py; this define is not in the headers 35 inline constexpr uint64_t kMaxAvbMetadataSize = 69632ul; 36 37 struct ChainPartition { 38 std::string name; 39 std::string rollback_index; 40 std::string key_path; 41 }; 42 43 class Avb { 44 public: 45 Avb(std::string avbtool_path); 46 Avb(std::string avbtool_path, std::string algorithm, std::string key); 47 48 /** 49 * AddHashFooter - sign and add hash footer to the partition for 50 * avb and dm-verity verification 51 * 52 * @image_path: path to image to sign 53 * @partition_name: partition name (without A/B suffix) 54 * @partition_size_bytes: partition size (in bytes) 55 */ 56 Result<void> AddHashFooter(const std::string& image_path, 57 const std::string& partition_name, 58 const off_t partition_size_bytes) const; 59 Result<void> WriteInfoImage(const std::string& image_path, 60 const std::string& output_path) const; 61 Result<void> MakeVbMetaImage( 62 const std::string& output_path, 63 const std::vector<ChainPartition>& chained_partitions, 64 const std::vector<std::string>& included_partitions, 65 const std::vector<std::string>& extra_arguments); 66 67 private: 68 Command GenerateAddHashFooter(const std::string& image_path, 69 const std::string& partition_name, 70 const off_t partition_size_bytes) const; 71 Command GenerateInfoImage(const std::string& image_path, 72 const SharedFD& output_path) const; 73 Command GenerateMakeVbMetaImage( 74 const std::string& output_path, 75 const std::vector<ChainPartition>& chained_partitions, 76 const std::vector<std::string>& included_partitions, 77 const std::vector<std::string>& extra_arguments); 78 79 std::string avbtool_path_; 80 std::string algorithm_; 81 std::string key_; 82 }; 83 84 Result<void> EnforceVbMetaSize(const std::string& path); 85 86 std::unique_ptr<Avb> GetDefaultAvb(); 87 88 fruit::Component<Avb> CuttlefishKeyAvbComponent(); 89 90 } // namespace cuttlefish 91