• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FsAvbHashDescriptor : AvbHashDescriptor {
59     std::string partition_name;
60     std::string salt;
61     std::string digest;
62 };
63 
64 struct FsAvbHashtreeDescriptor : AvbHashtreeDescriptor {
65     std::string partition_name;
66     std::string salt;
67     std::string root_digest;
68 };
69 
70 class VBMetaData {
71   public:
72     // Constructors
VBMetaData()73     VBMetaData() : vbmeta_ptr_(nullptr), vbmeta_size_(0){};
74 
VBMetaData(const uint8_t * data,size_t size,const std::string & partition_name)75     VBMetaData(const uint8_t* data, size_t size, const std::string& partition_name)
76         : vbmeta_ptr_(new (std::nothrow) uint8_t[size]),
77           vbmeta_size_(size),
78           partition_name_(partition_name) {
79         // The ownership of data is NOT transferred, i.e., the caller still
80         // needs to release the memory as we make a copy here.
81         std::memcpy(vbmeta_ptr_.get(), data, size * sizeof(uint8_t));
82     }
83 
VBMetaData(size_t size,const std::string & partition_name)84     explicit VBMetaData(size_t size, const std::string& partition_name)
85         : vbmeta_ptr_(new (std::nothrow) uint8_t[size]),
86           vbmeta_size_(size),
87           partition_name_(partition_name) {}
88 
89     // Extracts vbmeta header from the vbmeta buffer, set update_vbmeta_size to
90     // true to update vbmeta_size_ to the actual size with valid content.
91     std::unique_ptr<AvbVBMetaImageHeader> GetVBMetaHeader(bool update_vbmeta_size = false);
92 
93     // Sets the vbmeta_path where we load the vbmeta data. Could be a partition or a file.
94     // e.g.,
95     // - /dev/block/by-name/system_a
96     // - /path/to/system_other.img.
set_vbmeta_path(std::string vbmeta_path)97     void set_vbmeta_path(std::string vbmeta_path) { vbmeta_path_ = std::move(vbmeta_path); }
98 
99     // Get methods for each data member.
partition()100     const std::string& partition() const { return partition_name_; }
vbmeta_path()101     const std::string& vbmeta_path() const { return vbmeta_path_; }
data()102     uint8_t* data() const { return vbmeta_ptr_.get(); }
size()103     const size_t& size() const { return vbmeta_size_; }
104 
105     // Maximum size of a vbmeta data - 64 KiB.
106     static const size_t kMaxVBMetaSize = 64 * 1024;
107 
108   private:
109     std::unique_ptr<uint8_t[]> vbmeta_ptr_;
110     size_t vbmeta_size_;
111     std::string partition_name_;
112     std::string vbmeta_path_;
113 };
114 
115 }  // namespace fs_mgr
116 }  // namespace android
117