• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
18 #ifndef ANDROID_VINTF_HAL_MANIFEST_H
19 #define ANDROID_VINTF_HAL_MANIFEST_H
20 
21 #include <utils/Errors.h>
22 #include <map>
23 #include <optional>
24 #include <string>
25 #include <vector>
26 
27 #include <hidl/metadata.h>
28 
29 #include "CheckFlags.h"
30 #include "FileSystem.h"
31 #include "HalGroup.h"
32 #include "KernelInfo.h"
33 #include "Level.h"
34 #include "ManifestHal.h"
35 #include "ManifestInstance.h"
36 #include "MapValueIterator.h"
37 #include "SchemaType.h"
38 #include "SystemSdk.h"
39 #include "VendorNdk.h"
40 #include "Version.h"
41 #include "Vndk.h"
42 #include "WithFileName.h"
43 #include "XmlFileGroup.h"
44 #include "constants.h"
45 
46 namespace android {
47 namespace vintf {
48 
49 struct MatrixHal;
50 struct CompatibilityMatrix;
51 
52 namespace details {
53 using InstancesOfVersion =
54     std::map<std::string /* interface */, std::set<std::string /* instance */>>;
55 using Instances = std::map<Version, InstancesOfVersion>;
56 
57 class CheckVintfUtils;
58 class FmOnlyVintfObject;
59 
60 }  // namespace details
61 
62 // A HalManifest is reported by the hardware and query-able from
63 // framework code. This is the API for the framework.
64 struct HalManifest : public HalGroup<ManifestHal>,
65                      public XmlFileGroup<ManifestXmlFile>,
66                      public WithFileName {
67    public:
68 
69     // Construct a device HAL manifest.
HalManifestHalManifest70     HalManifest() : mType(SchemaType::DEVICE) {}
71 
72     bool add(ManifestHal&& hal, std::string* error = nullptr);
73     // Move all hals from another HalManifest to this.
74     bool addAllHals(HalManifest* other, std::string* error = nullptr);
75 
76     // Given a component name (e.g. "android.hardware.camera"),
77     // return getHal(name)->transport if the component exist and v exactly matches
78     // one of the versions in that component, else EMPTY
79     Transport getHidlTransport(const std::string& name, const Version& v,
80                                const std::string& interfaceName,
81                                const std::string& instanceName) const;
82 
83     // Check compatibility against a compatibility matrix. Considered compatible if
84     // - framework manifest vs. device compat-mat
85     //     - checkIncompatibility for HALs returns only optional HALs
86     //     - one of manifest.vndk match compat-mat.vndk
87     // - device manifest vs. framework compat-mat
88     //     - checkIncompatibility for HALs returns only optional HALs
89     //     - manifest.sepolicy.version match one of compat-mat.sepolicy.sepolicy-version
90     bool checkCompatibility(const CompatibilityMatrix& mat, std::string* error = nullptr,
91                             CheckFlags::Type flags = CheckFlags::DEFAULT) const;
92 
93     // Generate a compatibility matrix such that checkCompatibility will return true.
94     CompatibilityMatrix generateCompatibleMatrix(bool optional = true) const;
95 
96     // Returns all component names.
97     std::set<std::string> getHalNames() const;
98 
99     // Returns all component names and versions, e.g.
100     // "android.hardware.camera.device@1.0", "android.hardware.camera.device@3.2",
101     // "android.hardware.nfc@1.0"]
102     // For AIDL HALs, versions are stripped away.
103     std::set<std::string> getHalNamesAndVersions() const;
104 
105     // Type of the manifest. FRAMEWORK or DEVICE.
106     SchemaType type() const;
107     void setType(SchemaType type);
108 
109     // FCM version that it implements.
110     Level level() const;
111 
112     // device.mSepolicyVersion. Assume type == device.
113     // Abort if type != device.
114     const Version &sepolicyVersion() const;
115 
116     // framework.mVendorNdks. Assume type == framework.
117     // Abort if type != framework.
118     const std::vector<VendorNdk>& vendorNdks() const;
119 
120     // If the corresponding <xmlfile> with the given version exists,
121     // - Return the overridden <path> if it is present,
122     // - otherwise the default value: /{system,vendor}/etc/<name>_V<major>_<minor>.xml
123     // Otherwise if the <xmlfile> entry does not exist, "" is returned.
124     std::string getXmlFilePath(const std::string& xmlFileName, const Version& version) const;
125 
126     // Alternative to forEachInstance if you just need a set of instance names instead.
127     std::set<std::string> getHidlInstances(const std::string& package, const Version& version,
128                                            const std::string& interfaceName) const;
129     std::set<std::string> getAidlInstances(const std::string& package, size_t version,
130                                            const std::string& interfaceName) const;
131     std::set<std::string> getAidlInstances(const std::string& package,
132                                            const std::string& interfaceName) const;
133 
134     // Return whether instance is in getHidlInstances(...).
135     bool hasHidlInstance(const std::string& package, const Version& version,
136                          const std::string& interfaceName, const std::string& instance) const;
137 
138     // Return whether a given AIDL instance is in this manifest with version >= the given version.
139     bool hasAidlInstance(const std::string& package, size_t version,
140                          const std::string& interfaceName, const std::string& instance) const;
141 
142     // Return whether a given AIDL instance is in this manifest with any version.
143     bool hasAidlInstance(const std::string& package, const std::string& interfaceName,
144                          const std::string& instance) const;
145 
146     // Insert the given instance. After inserting it, the instance will be available via
147     // forEachInstance* functions. This modifies the manifest.
148     // Return whether this operation is successful.
149     bool insertInstance(const FqInstance& fqInstance, Transport transport, Arch arch, HalFormat fmt,
150                         std::string* error = nullptr);
151 
152     // Add everything from another manifest. If no errors (return true), it is guaranteed
153     // that other->empty() == true after execution.
154     [[nodiscard]] bool addAll(HalManifest* other, std::string* error = nullptr);
155 
156    protected:
157     // Check before add()
158     bool shouldAdd(const ManifestHal& toAdd, std::string* error) const;
159     bool shouldAddXmlFile(const ManifestXmlFile& toAdd) const override;
160 
161     bool forEachInstanceOfVersion(
162         HalFormat format, const std::string& package, const Version& expectVersion,
163         const std::function<bool(const ManifestInstance&)>& func) const override;
164 
165    private:
166     friend struct HalManifestConverter;
167     friend class VintfObject;
168     friend class AssembleVintfImpl;
169     friend class details::CheckVintfUtils;
170     friend struct LibVintfTest;
171     friend class details::FmOnlyVintfObject;
172     friend std::string dump(const HalManifest &vm);
173     friend bool operator==(const HalManifest &lft, const HalManifest &rgt);
174 
175     status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
176                                  std::string* error = nullptr);
177 
178     details::Instances expandInstances(const std::string& name) const;
179     // Check if all instances in matrixHal is supported in this manifest.
180     bool isCompatible(const details::Instances& instances, const MatrixHal& matrixHal) const;
181 
182     // Return a list of error messages (for each <hal> name) that does NOT conform to
183     // the given compatibility matrix. It does not contain components that are optional.
184     // That is, return empty list iff
185     // (instance in matrix) => (instance in manifest).
186     std::vector<std::string> checkIncompatibleHals(const CompatibilityMatrix& mat) const;
187 
188     // Return vector of instance names that are defined in an APEX that are not
189     // specified as updatable apex hals in the compatibility matrix.
190     std::vector<std::string> checkApexHals(const CompatibilityMatrix& mat) const;
191 
192     void removeHals(const std::string& name, size_t majorVer);
193 
194     // Returns a list of instance names that are in this manifest but
195     // are not specified in the given matrix, whether the HAL is specified as an optional or
196     // required HAL.
197     // That is, return empty list iff
198     // (instance in manifest) => (instance in matrix).
199     std::set<std::string> checkUnusedHals(
200         const CompatibilityMatrix& mat,
201         const std::vector<HidlInterfaceMetadata>& hidlMetadata) const;
202 
203     // Check that manifest has no entries.
204     bool empty() const;
205 
206     // Alternative to forEachInstance if you just need a set of instance names instead.
207     std::set<std::string> getInstances(HalFormat format, const std::string& package,
208                                        const Version& version,
209                                        const std::string& interfaceName) const;
210 
211     // Return whether instance is in getInstances(...).
212     bool hasInstance(HalFormat format, const std::string& package, const Version& version,
213                      const std::string& interfaceName, const std::string& instance) const;
214 
215     // Get the <kernel> tag. Assumes type() == DEVICE.
216     // - On host, <kernel> tag only exists for the fully assembled HAL manifest.
217     // - On device, this only contain information about level(). Other information should be
218     //   looked up via RuntimeInfo.
219     const std::optional<KernelInfo>& kernel() const;
220 
221     // Merge information of other to this.
222     bool mergeKernel(std::optional<KernelInfo>* other, std::string* error = nullptr);
223 
224     // Whether the manifest contains information about the kernel for compatibility checks.
225     // True if kernel()->checkCompatibility can be called.
226     bool shouldCheckKernelCompatibility() const;
227 
228     // Helper for shouldAdd(). Check if |hal| has a conflicting major version with this. Return
229     // false if hal should not be added, and set |error| accordingly. Return true if check passes.
230     bool addingConflictingMajorVersion(const ManifestHal& hal, std::string* error) const;
231 
232     // Helper for shouldAdd(). Check if |hal| has a conflicting major version in <fqname> with this.
233     // Return false if hal should not be added, and set |error| accordingly. Return true if check
234     // passes.
235     bool addingConflictingFqInstance(const ManifestHal& hal, std::string* error) const;
236 
237     // Inferred kernel level.
238     Level inferredKernelLevel() const;
239 
240     SchemaType mType;
241     Level mLevel = Level::UNSPECIFIED;
242 
243     // The metaversion on the source file if the HAL manifest is parsed from an XML file,
244     // Otherwise, the object is created programmatically, so default to libvintf meta version.
245     Version mSourceMetaVersion = kMetaVersion;
246 
247     // entries for device hal manifest only
248     struct {
249         Version mSepolicyVersion;
250         std::optional<KernelInfo> mKernel;
251     } device;
252 
253     // entries for framework hal manifest only
254     struct {
255 #pragma clang diagnostic push
256 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
257         std::vector<Vndk> mVndks;
258 #pragma clang diagnostic pop
259 
260         std::vector<VendorNdk> mVendorNdks;
261         SystemSdk mSystemSdk;
262     } framework;
263 };
264 
265 } // namespace vintf
266 } // namespace android
267 
268 #endif // ANDROID_VINTF_HAL_MANIFEST_H
269