• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2018 Google, Inc
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 #ifndef _LIBDM_DMTARGET_H_
18 #define _LIBDM_DMTARGET_H_
19 
20 #include <linux/dm-ioctl.h>
21 #include <stdint.h>
22 
23 #include <string>
24 #include <vector>
25 
26 namespace android {
27 namespace dm {
28 
29 class DmTargetTypeInfo {
30   public:
DmTargetTypeInfo()31     DmTargetTypeInfo() : major_(0), minor_(0), patch_(0) {}
DmTargetTypeInfo(const struct dm_target_versions * info)32     DmTargetTypeInfo(const struct dm_target_versions* info)
33         : name_(info->name),
34           major_(info->version[0]),
35           minor_(info->version[1]),
36           patch_(info->version[2]) {}
37 
name()38     const std::string& name() const { return name_; }
version()39     std::string version() const {
40         return std::to_string(major_) + "." + std::to_string(minor_) + "." + std::to_string(patch_);
41     }
42 
43   private:
44     std::string name_;
45     uint32_t major_;
46     uint32_t minor_;
47     uint32_t patch_;
48 };
49 
50 class DmTarget {
51   public:
DmTarget(uint64_t start,uint64_t length)52     DmTarget(uint64_t start, uint64_t length) : start_(start), length_(length) {}
53 
54     virtual ~DmTarget() = default;
55 
56     // Returns name of the target.
57     virtual std::string name() const = 0;
58 
59     // Return the first logical sector represented by this target.
start()60     uint64_t start() const { return start_; }
61 
62     // Returns size in number of sectors when this target is part of
63     // a DmTable, return 0 otherwise.
size()64     uint64_t size() const { return length_; }
65 
66     // Function that converts this object to a string of arguments that can
67     // be passed to the kernel for adding this target in a table. Each target (e.g. verity, linear)
68     // must implement this, for it to be used on a device.
69     std::string Serialize() const;
70 
Valid()71     virtual bool Valid() const { return true; }
72 
73   protected:
74     // Get the parameter string that is passed to the end of the dm_target_spec
75     // for this target type.
76     virtual std::string GetParameterString() const = 0;
77 
78   private:
79     // logical sector number start and total length (in terms of 512-byte sectors) represented
80     // by this target within a DmTable.
81     uint64_t start_, length_;
82 };
83 
84 class DmTargetZero final : public DmTarget {
85   public:
DmTargetZero(uint64_t start,uint64_t length)86     DmTargetZero(uint64_t start, uint64_t length) : DmTarget(start, length) {}
87 
name()88     std::string name() const override { return "zero"; }
89     std::string GetParameterString() const override;
90 };
91 
92 class DmTargetLinear final : public DmTarget {
93   public:
DmTargetLinear(uint64_t start,uint64_t length,const std::string & block_device,uint64_t physical_sector)94     DmTargetLinear(uint64_t start, uint64_t length, const std::string& block_device,
95                    uint64_t physical_sector)
96         : DmTarget(start, length), block_device_(block_device), physical_sector_(physical_sector) {}
97 
name()98     std::string name() const override { return "linear"; }
99     std::string GetParameterString() const override;
block_device()100     const std::string& block_device() const { return block_device_; }
101 
102   private:
103     std::string block_device_;
104     uint64_t physical_sector_;
105 };
106 
107 class DmTargetVerity final : public DmTarget {
108   public:
109     DmTargetVerity(uint64_t start, uint64_t length, uint32_t version,
110                    const std::string& block_device, const std::string& hash_device,
111                    uint32_t data_block_size, uint32_t hash_block_size, uint32_t num_data_blocks,
112                    uint32_t hash_start_block, const std::string& hash_algorithm,
113                    const std::string& root_digest, const std::string& salt);
114 
115     void UseFec(const std::string& device, uint32_t num_roots, uint32_t num_blocks, uint32_t start);
116     void SetVerityMode(const std::string& mode);
117     void IgnoreZeroBlocks();
118 
name()119     std::string name() const override { return "verity"; }
120     std::string GetParameterString() const override;
Valid()121     bool Valid() const override { return valid_; }
122 
123   private:
124     std::vector<std::string> base_args_;
125     std::vector<std::string> optional_args_;
126     bool valid_;
127 };
128 
129 class DmTargetAndroidVerity final : public DmTarget {
130   public:
DmTargetAndroidVerity(uint64_t start,uint64_t length,const std::string & block_device,const std::string & keyid)131     DmTargetAndroidVerity(uint64_t start, uint64_t length, const std::string& block_device,
132                           const std::string& keyid)
133         : DmTarget(start, length), keyid_(keyid), block_device_(block_device) {}
134 
name()135     std::string name() const override { return "android-verity"; }
136     std::string GetParameterString() const override;
137 
138   private:
139     std::string keyid_;
140     std::string block_device_;
141 };
142 
143 // This is the same as DmTargetVerity, but the table may be specified as a raw
144 // string. This code exists only for fs_mgr_verity and should be avoided. Use
145 // DmTargetVerity for new code instead.
146 class DmTargetVerityString final : public DmTarget {
147   public:
DmTargetVerityString(uint64_t start,uint64_t length,const std::string & target_string)148     DmTargetVerityString(uint64_t start, uint64_t length, const std::string& target_string)
149         : DmTarget(start, length), target_string_(target_string) {}
150 
name()151     std::string name() const override { return "verity"; }
GetParameterString()152     std::string GetParameterString() const override { return target_string_; }
Valid()153     bool Valid() const override { return true; }
154 
155   private:
156     std::string target_string_;
157 };
158 
159 // dm-bow is the backup on write target that can provide checkpoint capability
160 // for file systems that do not support checkpoints natively
161 class DmTargetBow final : public DmTarget {
162   public:
DmTargetBow(uint64_t start,uint64_t length,const std::string & target_string)163     DmTargetBow(uint64_t start, uint64_t length, const std::string& target_string)
164         : DmTarget(start, length), target_string_(target_string) {}
165 
name()166     std::string name() const override { return "bow"; }
GetParameterString()167     std::string GetParameterString() const override { return target_string_; }
168 
169   private:
170     std::string target_string_;
171 };
172 
173 }  // namespace dm
174 }  // namespace android
175 
176 #endif /* _LIBDM_DMTARGET_H_ */
177