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 #ifndef ANDROID_VINTF_COMPATIBILITY_MATRIX_H
18 #define ANDROID_VINTF_COMPATIBILITY_MATRIX_H
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include <utils/Errors.h>
25 
26 #include "FileSystem.h"
27 #include "HalGroup.h"
28 #include "Level.h"
29 #include "MapValueIterator.h"
30 #include "MatrixHal.h"
31 #include "MatrixInstance.h"
32 #include "MatrixKernel.h"
33 #include "SchemaType.h"
34 #include "Sepolicy.h"
35 #include "SystemSdk.h"
36 #include "VendorNdk.h"
37 #include "Vndk.h"
38 #include "WithFileName.h"
39 #include "XmlFileGroup.h"
40 
41 namespace android {
42 namespace vintf {
43 
44 namespace details {
45 class CheckVintfUtils;
46 }  // namespace details
47 
48 // Compatibility matrix defines what hardware does the framework requires.
49 struct CompatibilityMatrix : public HalGroup<MatrixHal>,
50                              public XmlFileGroup<MatrixXmlFile>,
51                              public WithFileName {
52     // Create a framework compatibility matrix.
CompatibilityMatrixCompatibilityMatrix53     CompatibilityMatrix() : mType(SchemaType::FRAMEWORK) {}
54 
55     SchemaType type() const;
56     Level level() const;
57 
58     // If the corresponding <xmlfile> with the given version exists, for the first match,
59     // - Return the overridden <path> if it is present,
60     // - otherwise the default value: /{system,vendor}/etc/<name>_V<major>_<minor-max>.<format>
61     // Otherwise if the <xmlfile> entry does not exist, "" is returned.
62     // For example, if the matrix says ["audio@1.0-5" -> "foo.xml", "audio@1.3-7" -> bar.xml]
63     // getXmlSchemaPath("audio", 1.0) -> foo.xml
64     // getXmlSchemaPath("audio", 1.5) -> foo.xml
65     // getXmlSchemaPath("audio", 1.7) -> bar.xml
66     // (Normally, version ranges do not overlap, and the only match is returned.)
67     std::string getXmlSchemaPath(const std::string& xmlFileName, const Version& version) const;
68 
69     std::string getVendorNdkVersion() const;
70 
71     std::vector<VersionRange> getSepolicyVersions() const;
72 
73     bool add(MatrixHal&&, std::string* error = nullptr) override;
74     // Move all hals from another CompatibilityMatrix to this.
75     bool addAllHals(CompatibilityMatrix* other, std::string* error = nullptr);
76 
77    protected:
78     bool forEachInstanceOfVersion(
79         HalFormat format, const std::string& package, const Version& expectVersion,
80         const std::function<bool(const MatrixInstance&)>& func) const override;
81 
82    private:
83     // Add everything in inputMatrix to "this" as requirements.
84     bool addAll(CompatibilityMatrix* inputMatrix, std::string* error);
85 
86     // Add all <kernel> from other to "this". Error if there is a conflict.
87     bool addAllKernels(CompatibilityMatrix* other, std::string* error);
88 
89     // Add a <kernel> tag to "this". Error if there is a conflict.
90     bool addKernel(MatrixKernel&& kernel, std::string* error);
91 
92     // Merge <sepolicy> with other's <sepolicy>. Error if there is a conflict.
93     bool addSepolicy(CompatibilityMatrix* other, std::string* error);
94 
95     // Merge <avb><vbmeta-version> with other's <avb><vbmeta-version>. Error if there is a conflict.
96     bool addAvbMetaVersion(CompatibilityMatrix* other, std::string* error);
97 
98     // Merge <vndk> with other's <vndk>. Error if there is a conflict.
99     bool addVndk(CompatibilityMatrix* other, std::string* error);
100 
101     // Merge <vendor-ndk> with other's <vendor-ndk>. Error if there is a conflict.
102     bool addVendorNdk(CompatibilityMatrix* other, std::string* error);
103 
104     // Merge <system-sdk> with other's <system-sdk>.
105     bool addSystemSdk(CompatibilityMatrix* other, std::string* error);
106 
107     // Add everything in inputMatrix to "this" as optional.
108     bool addAllAsOptional(CompatibilityMatrix* inputMatrix, std::string* error);
109 
110     // Add all HALs as optional HALs from "other". This function moves MatrixHal objects
111     // from "other".
112     // Require other->level() > this->level(), otherwise do nothing.
113     bool addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error);
114 
115     // Similar to addAllHalsAsOptional but on <xmlfile> entries.
116     bool addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error);
117 
118     // Combine a set of framework compatibility matrices. For each CompatibilityMatrix in matrices
119     // (in the order of level(), where UNSPECIFIED (empty) is treated as deviceLevel)
120     // - If level() < deviceLevel, ignore
121     // - If level() == UNSPECIFIED or level() == deviceLevel,
122     //   - Add as hard requirements. See combineSameFcmVersion
123     // - If level() > deviceLevel,
124     //   - all <hal> versions and <xmlfile>s are added as optional.
125     //   - <kernel minlts="x.y.z"> is added only if x.y does not exist in a file
126     //     with lower level()
127     //   - <sepolicy>, <avb><vbmeta-version> is ignored
128     // Return the combined matrix, nullptr if any error (e.g. conflict of information).
129     static std::unique_ptr<CompatibilityMatrix> combine(Level deviceLevel,
130                                                         std::vector<CompatibilityMatrix>* matrices,
131                                                         std::string* error);
132 
133     // Combine a set of device compatibility matrices.
134     static std::unique_ptr<CompatibilityMatrix> combineDeviceMatrices(
135         std::vector<CompatibilityMatrix>* matrices, std::string* error);
136 
137     status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
138                                  std::string* error = nullptr);
139 
140     MatrixHal* splitInstance(MatrixHal* existingHal, const std::string& interface,
141                              const std::string& instance, bool isRegex);
142 
143     // Return whether instance is in "this"; that is, instance is in any <instance> tag or
144     // matches any <regex-instance> tag.
145     bool matchInstance(HalFormat format, const std::string& halName, const Version& version,
146                        const std::string& interfaceName, const std::string& instance) const;
147 
148     // Return the level of the matrixKernel object that it is originally from.
149     // Prerequisite: matrixKernel is in mKernels.
150     Level getSourceMatrixLevel(const MatrixKernel* matrixKernel) const;
151 
152     friend struct HalManifest;
153     friend struct RuntimeInfo;
154     friend struct CompatibilityMatrixConverter;
155     friend struct LibVintfTest;
156     friend struct FrameworkCompatibilityMatrixCombineTest;
157     friend struct DeviceCompatibilityMatrixCombineTest;
158     friend class VintfObject;
159     friend class AssembleVintfImpl;
160     friend class KernelInfo;
161     friend class details::CheckVintfUtils;
162     friend bool operator==(const CompatibilityMatrix &, const CompatibilityMatrix &);
163 
164     SchemaType mType;
165     Level mLevel = Level::UNSPECIFIED;
166 
167     // entries only for framework compatibility matrix.
168     struct {
169         std::vector<MatrixKernel> mKernels;
170         Sepolicy mSepolicy;
171         Version mAvbMetaVersion;
172     } framework;
173 
174     // entries only for device compatibility matrix.
175     struct {
176 #pragma clang diagnostic push
177 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
178         Vndk mVndk;
179 #pragma clang diagnostic pop
180 
181         VendorNdk mVendorNdk;
182         SystemSdk mSystemSdk;
183     } device;
184 };
185 
186 } // namespace vintf
187 } // namespace android
188 
189 #endif // ANDROID_VINTF_COMPATIBILITY_MATRIX_H
190