1 /* 2 * Copyright (C) 2019 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 #pragma once 18 19 #include <cstring> 20 #include <memory> 21 #include <ostream> 22 23 #include <libavb/libavb.h> 24 25 namespace android { 26 namespace fs_mgr { 27 28 enum class VBMetaVerifyResult { 29 kSuccess = 0, 30 kError = 1, 31 kErrorVerification = 2, 32 }; 33 34 std::ostream& operator<<(std::ostream& os, VBMetaVerifyResult); 35 36 enum class AvbHashtreeResult { 37 kSuccess = 0, 38 kFail, 39 kDisabled, 40 }; 41 42 enum class HashAlgorithm { 43 kInvalid = 0, 44 kSHA256 = 1, 45 kSHA512 = 2, 46 }; 47 48 enum class AvbHandleStatus { 49 kSuccess = 0, 50 kUninitialized = 1, 51 kHashtreeDisabled = 2, 52 kVerificationDisabled = 3, 53 kVerificationError = 4, 54 }; 55 56 std::ostream& operator<<(std::ostream& os, AvbHandleStatus status); 57 58 struct FsAvbHashtreeDescriptor : AvbHashtreeDescriptor { 59 std::string partition_name; 60 std::string salt; 61 std::string root_digest; 62 }; 63 64 class VBMetaData { 65 public: 66 // Constructors VBMetaData()67 VBMetaData() : vbmeta_ptr_(nullptr), vbmeta_size_(0){}; 68 VBMetaData(const uint8_t * data,size_t size,const std::string & partition_name)69 VBMetaData(const uint8_t* data, size_t size, const std::string& partition_name) 70 : vbmeta_ptr_(new (std::nothrow) uint8_t[size]), 71 vbmeta_size_(size), 72 partition_name_(partition_name) { 73 // The ownership of data is NOT transferred, i.e., the caller still 74 // needs to release the memory as we make a copy here. 75 std::memcpy(vbmeta_ptr_.get(), data, size * sizeof(uint8_t)); 76 } 77 VBMetaData(size_t size,const std::string & partition_name)78 explicit VBMetaData(size_t size, const std::string& partition_name) 79 : vbmeta_ptr_(new (std::nothrow) uint8_t[size]), 80 vbmeta_size_(size), 81 partition_name_(partition_name) {} 82 83 // Extracts vbmeta header from the vbmeta buffer, set update_vbmeta_size to 84 // true to update vbmeta_size_ to the actual size with valid content. 85 std::unique_ptr<AvbVBMetaImageHeader> GetVBMetaHeader(bool update_vbmeta_size = false); 86 87 // Sets the vbmeta_path where we load the vbmeta data. Could be a partition or a file. 88 // e.g., 89 // - /dev/block/by-name/system_a 90 // - /path/to/system_other.img. set_vbmeta_path(std::string vbmeta_path)91 void set_vbmeta_path(std::string vbmeta_path) { vbmeta_path_ = std::move(vbmeta_path); } 92 93 // Get methods for each data member. partition()94 const std::string& partition() const { return partition_name_; } vbmeta_path()95 const std::string& vbmeta_path() const { return vbmeta_path_; } data()96 uint8_t* data() const { return vbmeta_ptr_.get(); } size()97 const size_t& size() const { return vbmeta_size_; } 98 99 // Maximum size of a vbmeta data - 64 KiB. 100 static const size_t kMaxVBMetaSize = 64 * 1024; 101 102 private: 103 std::unique_ptr<uint8_t[]> vbmeta_ptr_; 104 size_t vbmeta_size_; 105 std::string partition_name_; 106 std::string vbmeta_path_; 107 }; 108 109 } // namespace fs_mgr 110 } // namespace android 111