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<SepolicyVersionRange> getSepolicyVersions() const;
72 
73     bool add(MatrixHal&&, std::string* error = nullptr);
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:
121     //    - If kernelLevel is UNSPECIFIED, ignore
122     //    - If kernelLevel is not UNSPECIFIED and level() < kernelLevel, ignore
123     //    - If kernelLevel is not UNSPECIFIED and level() >= kernelLevel, add kernel()
124     // - If level() == UNSPECIFIED or level() == deviceLevel,
125     //   - Add as hard requirements. See combineSameFcmVersion
126     // - If level() > deviceLevel,
127     //   - all <hal> versions and <xmlfile>s are added as optional.
128     //   - <kernel minlts="x.y.z"> is added only if x.y does not exist in a file
129     //     with lower level()
130     //   - <sepolicy>, <avb><vbmeta-version> is ignored
131     // Return the combined matrix, nullptr if any error (e.g. conflict of information).
132     static std::unique_ptr<CompatibilityMatrix> combine(Level deviceLevel, Level kernelLevel,
133                                                         std::vector<CompatibilityMatrix>* matrices,
134                                                         std::string* error);
135 
136     // Combine a set of device compatibility matrices.
137     static std::unique_ptr<CompatibilityMatrix> combineDeviceMatrices(
138         std::vector<CompatibilityMatrix>* matrices, std::string* error);
139 
140     status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
141                                  std::string* error = nullptr);
142 
143     MatrixHal* splitInstance(MatrixHal* existingHal, const std::string& interface,
144                              const std::string& instance, bool isRegex);
145 
146     // Return whether instance is in "this"; that is, instance is in any <instance> tag or
147     // matches any <regex-instance> tag.
148     bool matchInstance(HalFormat format, const std::string& halName, const Version& version,
149                        const std::string& interfaceName, const std::string& instance) const;
150 
151     // Return the minlts of the latest <kernel>, or empty value if any error (e.g. this is not an
152     // FCM, or there are no <kernel> tags).
153     [[nodiscard]] KernelVersion getLatestKernelMinLts() const;
154 
155     friend struct HalManifest;
156     friend struct RuntimeInfo;
157     friend struct CompatibilityMatrixConverter;
158     friend struct LibVintfTest;
159     friend struct FrameworkCompatibilityMatrixCombineTest;
160     friend struct DeviceCompatibilityMatrixCombineTest;
161     friend class VintfObject;
162     friend class AssembleVintfImpl;
163     friend class KernelInfo;
164     friend class details::CheckVintfUtils;
165     friend bool operator==(const CompatibilityMatrix &, const CompatibilityMatrix &);
166 
167     SchemaType mType;
168     Level mLevel = Level::UNSPECIFIED;
169 
170     // entries only for framework compatibility matrix.
171     struct {
172         std::vector<MatrixKernel> mKernels;
173         Sepolicy mSepolicy;
174         Version mAvbMetaVersion;
175     } framework;
176 
177     // entries only for device compatibility matrix.
178     struct {
179 #pragma clang diagnostic push
180 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
181         Vndk mVndk;
182 #pragma clang diagnostic pop
183 
184         VendorNdk mVendorNdk;
185         SystemSdk mSystemSdk;
186     } device;
187 };
188 
189 } // namespace vintf
190 } // namespace android
191 
192 #endif // ANDROID_VINTF_COMPATIBILITY_MATRIX_H
193