blob: 290ebf9907ebc03e4507dc59801272650a76116c [file] [log] [blame]
Yifan Hong676447a2016-11-15 12:57:23 -08001/*
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#include "CompatibilityMatrix.h"
18
Yifan Hongb6c7f492018-02-27 14:07:57 -080019#include <iostream>
Yifan Hongdbe9db32017-12-11 19:06:11 -080020#include <utility>
21
Yifan Honge7837b12018-10-11 10:38:57 -070022#include <android-base/strings.h>
23
Yifan Hongbbfff302017-06-06 17:10:13 -070024#include "parse_string.h"
Yifan Hongddae77e2017-12-18 16:57:07 -080025#include "parse_xml.h"
Yifan Hongb6c7f492018-02-27 14:07:57 -080026#include "utils.h"
Yifan Hongddae77e2017-12-18 16:57:07 -080027
Yifan Hong676447a2016-11-15 12:57:23 -080028namespace android {
29namespace vintf {
30
Yifan Honge7837b12018-10-11 10:38:57 -070031bool CompatibilityMatrix::addKernel(MatrixKernel&& kernel, std::string* error) {
Yifan Hong7c7d7062017-04-04 16:26:51 -070032 if (mType != SchemaType::FRAMEWORK) {
Yifan Honge7837b12018-10-11 10:38:57 -070033 if (error) {
34 *error = "Cannot add <kernel> to a " + to_string(mType) + " compatibility matrix.";
35 }
Yifan Hong7c7d7062017-04-04 16:26:51 -070036 return false;
37 }
Yifan Honge7837b12018-10-11 10:38:57 -070038
39 auto it = framework.mKernels.begin();
40 for (; it != framework.mKernels.end(); ++it) {
41 if (it->minLts() == kernel.minLts()) {
42 break;
43 }
44 if (it->minLts().version == kernel.minLts().version &&
45 it->minLts().majorRev == kernel.minLts().majorRev) {
46 if (error) {
47 *error = "Kernel version mismatch; cannot add " + to_string(kernel.minLts()) +
48 " because " + to_string(it->minLts()) + " was added.";
49 }
50 return false;
51 }
52 }
53
54 bool seenVersion = it != framework.mKernels.end();
55
56 if (seenVersion) {
57 // If no conditions, must be the first among the same minLts
58 // because O libvintf only checks the first <kernel> tag that version matches.
59 if (kernel.conditions().empty()) {
60 // Found first <kernel> with the same minLts.
61 // Append config if it does not have <condition>s, else error.
62 if (it->conditions().empty()) {
63 const auto& configs = kernel.configs();
64 it->mConfigs.insert(it->mConfigs.end(), configs.begin(), configs.end());
65 } else {
66 if (error) {
67 *error =
68 "Base compatibility matrix has <condition> for the first <kernel> "
69 "with minlts " +
70 to_string(kernel.minLts()) + " for unknown reason.";
71 }
72 return false;
73 }
74 return true;
75 }
76 } else {
77 // First <kernel> of a minLts must not have <condition>'s for backwards compatibility
78 // with O libvintf.
79 if (!kernel.conditions().empty()) {
80 framework.mKernels.push_back(MatrixKernel(KernelVersion{kernel.minLts()}, {}));
81 }
82 }
83
Yifan Hong7c7d7062017-04-04 16:26:51 -070084 framework.mKernels.push_back(std::move(kernel));
Yifan Hong676447a2016-11-15 12:57:23 -080085 return true;
86}
87
Yifan Hong398f4c72017-04-13 20:18:01 -070088SchemaType CompatibilityMatrix::type() const {
89 return mType;
90}
91
Yifan Hong2027a492017-12-11 15:21:19 -080092Level CompatibilityMatrix::level() const {
93 return mLevel;
94}
95
Yifan Hongdb127cb2017-09-19 13:36:21 -070096Version CompatibilityMatrix::getMinimumMetaVersion() const {
97 // TODO(b/62801658): this needs to depend on whether there are 1.1 requirements
98 // (e.g. required <xmlfile> entry)
99 return {1, 0};
100}
Yifan Hong1e5a0542017-04-28 14:37:56 -0700101
Yifan Hong9f78c182018-07-12 14:45:52 -0700102status_t CompatibilityMatrix::fetchAllInformation(const FileSystem* fileSystem,
103 const std::string& path, std::string* error) {
104 return details::fetchAllInformation(fileSystem, path, gCompatibilityMatrixConverter, this,
105 error);
Yifan Hong1e5a0542017-04-28 14:37:56 -0700106}
107
Yifan Hongbbfff302017-06-06 17:10:13 -0700108std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName,
109 const Version& version) const {
110 using std::literals::string_literals::operator""s;
111 auto range = getXmlFiles(xmlFileName);
112 for (auto it = range.first; it != range.second; ++it) {
113 const MatrixXmlFile& matrixXmlFile = it->second;
114 if (matrixXmlFile.versionRange().contains(version)) {
115 if (!matrixXmlFile.overriddenPath().empty()) {
116 return matrixXmlFile.overriddenPath();
117 }
118 return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" +
119 xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) +
120 "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." +
121 to_string(matrixXmlFile.format());
122 }
123 }
124 return "";
125}
126
Yifan Honge7e45532018-03-16 18:11:49 -0700127// Split existingHal into a HAL that contains only interface/instance and a HAL
128// that does not contain it. Return the HAL that contains only interface/instance.
129// - Return nullptr if existingHal does not contain interface/instance
130// - Return existingHal if existingHal contains only interface/instance
131// - Remove interface/instance from existingHal, and return a new MatrixHal (that is added
132// to "this") that contains only interface/instance.
133MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface,
Yifan Hong643a9ef2018-03-21 14:13:55 -0700134 const std::string& instanceOrPattern, bool isRegex) {
135 bool found = false;
136 bool foundOthers = false;
137 existingHal->forEachInstance([&](const auto& matrixInstance) {
138 bool interfaceMatch = matrixInstance.interface() == interface;
139 bool instanceMatch = false;
140 if (matrixInstance.isRegex() && isRegex) {
141 instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern);
142 } else if (!matrixInstance.isRegex() && !isRegex) {
143 instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern);
144 }
145
146 bool match = interfaceMatch && instanceMatch;
147
148 found |= match;
149 foundOthers |= (!match);
150
151 return !found || !foundOthers;
152 });
153
154 if (!found) {
Yifan Honge7e45532018-03-16 18:11:49 -0700155 return nullptr;
156 }
157
Yifan Hong643a9ef2018-03-21 14:13:55 -0700158 if (!foundOthers) {
Yifan Honge7e45532018-03-16 18:11:49 -0700159 return existingHal;
160 }
161
Yifan Hong643a9ef2018-03-21 14:13:55 -0700162 existingHal->removeInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700163 MatrixHal copy = *existingHal;
164 copy.clearInstances();
Yifan Hong643a9ef2018-03-21 14:13:55 -0700165 copy.insertInstance(interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700166
167 return addInternal(std::move(copy));
168}
169
Yifan Hong7967d7b2018-03-15 17:08:58 -0700170// Add all package@other_version::interface/instance as an optional instance.
171// If package@this_version::interface/instance is in this (that is, some instance
172// with the same package and interface and instance exists), then other_version is
173// considered a possible replacement to this_version.
174// See LibVintfTest.AddOptionalHal* tests for details.
Yifan Hongdbe9db32017-12-11 19:06:11 -0800175bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) {
176 if (other == nullptr || other->level() <= level()) {
177 return true;
178 }
179
180 for (auto& pair : other->mHals) {
181 const std::string& name = pair.first;
182 MatrixHal& halToAdd = pair.second;
Yifan Hongdbe9db32017-12-11 19:06:11 -0800183
Yifan Honge7e45532018-03-16 18:11:49 -0700184 std::set<std::pair<std::string, std::string>> insertedInstances;
Yifan Hong643a9ef2018-03-21 14:13:55 -0700185 std::set<std::pair<std::string, std::string>> insertedRegex;
Yifan Honge7e45532018-03-16 18:11:49 -0700186 auto existingHals = getHals(name);
187
188 halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges,
Yifan Hong643a9ef2018-03-21 14:13:55 -0700189 const std::string& interface,
190 const std::string& instanceOrPattern, bool isRegex) {
Yifan Honge7e45532018-03-16 18:11:49 -0700191 for (auto* existingHal : existingHals) {
Yifan Hong643a9ef2018-03-21 14:13:55 -0700192 MatrixHal* splitInstance =
193 this->splitInstance(existingHal, interface, instanceOrPattern, isRegex);
Yifan Honge7e45532018-03-16 18:11:49 -0700194 if (splitInstance != nullptr) {
195 splitInstance->insertVersionRanges(versionRanges);
Yifan Hong643a9ef2018-03-21 14:13:55 -0700196 if (isRegex) {
197 insertedRegex.insert(std::make_pair(interface, instanceOrPattern));
198 } else {
199 insertedInstances.insert(std::make_pair(interface, instanceOrPattern));
200 }
Yifan Honge7e45532018-03-16 18:11:49 -0700201 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800202 }
Yifan Honge7e45532018-03-16 18:11:49 -0700203 return true;
204 });
205
206 // Add the remaining instances.
207 for (const auto& pair : insertedInstances) {
Yifan Hong643a9ef2018-03-21 14:13:55 -0700208 halToAdd.removeInstance(pair.first, pair.second, false /* isRegex */);
209 }
210 for (const auto& pair : insertedRegex) {
211 halToAdd.removeInstance(pair.first, pair.second, true /* isRegex */);
Yifan Hong7967d7b2018-03-15 17:08:58 -0700212 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800213
Yifan Hong7e9e04d2018-03-20 13:06:00 -0700214 if (halToAdd.instancesCount() > 0) {
Yifan Hong7967d7b2018-03-15 17:08:58 -0700215 halToAdd.setOptional(true);
216 if (!add(std::move(halToAdd))) {
217 if (error) {
218 *error = "Cannot add HAL " + name + " for unknown reason.";
Yifan Hongdbe9db32017-12-11 19:06:11 -0800219 }
220 return false;
221 }
Yifan Hongdbe9db32017-12-11 19:06:11 -0800222 }
223 }
224 return true;
225}
226
Yifan Hongd4b92fe2017-12-20 15:29:03 -0800227bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) {
228 if (other == nullptr || other->level() <= level()) {
229 return true;
230 }
231 for (auto& pair : other->mXmlFiles) {
232 const std::string& name = pair.first;
233 MatrixXmlFile& xmlFileToAdd = pair.second;
234
235 xmlFileToAdd.mOptional = true;
236 if (!addXmlFile(std::move(xmlFileToAdd))) {
237 if (error) {
238 *error = "Cannot add XML File " + name + " for unknown reason.";
239 }
240 return false;
241 }
242 }
243 return true;
244}
245
Yifan Honge7837b12018-10-11 10:38:57 -0700246// Merge Kernel.
247// Add <kernel> from exact "level", then optionally add <kernel> from high levels to low levels.
248// For example, (each letter is a kernel version x.y.z)
249// 1.xml: A1, B1
250// 2.xml: B2, C2, D2
251// 3.xml: D3, E3
252// Then the combined 1.xml should have
253// A1, B1 (from 1.xml, required), C2, D2, E3 (optional, use earliest possible).
254bool CompatibilityMatrix::addAllKernels(CompatibilityMatrix* other, std::string* error) {
255 for (MatrixKernel& kernel : other->framework.mKernels) {
256 KernelVersion ver = kernel.minLts();
257 if (!addKernel(std::move(kernel), error)) {
258 if (error) {
259 *error = "Cannot add kernel version " + to_string(ver) + ": " + *error;
260 }
261 return false;
262 }
263 }
264 return true;
265}
266
Yifan Hongd6de7f62018-04-26 18:40:02 -0700267bool CompatibilityMatrix::addAllKernelsAsOptional(CompatibilityMatrix* other, std::string* error) {
268 if (other == nullptr || other->level() <= level()) {
269 return true;
270 }
271
272 for (MatrixKernel& kernelToAdd : other->framework.mKernels) {
273 bool exists =
274 std::any_of(this->framework.mKernels.begin(), this->framework.mKernels.end(),
275 [&kernelToAdd](const MatrixKernel& existing) {
276 return kernelToAdd.minLts().version == existing.minLts().version &&
277 kernelToAdd.minLts().majorRev == existing.minLts().majorRev;
278 });
279
280 if (exists) {
281 // Shouldn't retroactively add requirements to minLts(), so ignore this.
282 // This happens even when kernelToAdd.conditions() != existing.conditions().
283 continue;
284 }
285
286 KernelVersion minLts = kernelToAdd.minLts();
Yifan Honge7837b12018-10-11 10:38:57 -0700287 if (!addKernel(std::move(kernelToAdd), error)) {
Yifan Hongd6de7f62018-04-26 18:40:02 -0700288 if (error) {
Yifan Honge7837b12018-10-11 10:38:57 -0700289 *error = "Cannot add " + to_string(minLts) + ": " + *error;
Yifan Hongd6de7f62018-04-26 18:40:02 -0700290 }
291 return false;
292 }
293 }
294 return true;
295}
296
Yifan Honge7837b12018-10-11 10:38:57 -0700297template <typename T>
298static bool mergeField(T* dst, T* src) {
299 static const T kEmpty{};
300 if (*dst == *src) {
301 return true; // no conflict
302 }
303 if (*src == kEmpty) {
304 return true;
305 }
306 if (*dst == kEmpty) {
307 *dst = std::move(*src);
308 return true;
309 }
310 return false;
311}
312
313bool CompatibilityMatrix::addSepolicy(CompatibilityMatrix* other, std::string* error) {
314 bool success = mergeField(&this->framework.mSepolicy, &other->framework.mSepolicy);
315 if (!success && error) *error = "<sepolicy> is already defined";
316 return success;
317}
318
319bool CompatibilityMatrix::addAvbMetaVersion(CompatibilityMatrix* other, std::string* error) {
320 bool success = mergeField(&this->framework.mAvbMetaVersion, &other->framework.mAvbMetaVersion);
321 if (!success && error) *error = "<avb><vbmeta-version> is already defined";
322 return success;
323}
324
Yifan Hongfb7469c2017-04-05 19:15:21 -0700325bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) {
Yifan Hong2027a492017-12-11 15:21:19 -0800326 return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals &&
327 lft.mXmlFiles == rgt.mXmlFiles &&
Yifan Hongfeb454e2018-01-09 16:16:40 -0800328 (lft.mType != SchemaType::DEVICE ||
329 (
Yifan Hong0f529fa2018-01-10 14:51:59 -0800330#pragma clang diagnostic push
331#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Yifan Hongfeb454e2018-01-09 16:16:40 -0800332 lft.device.mVndk == rgt.device.mVndk &&
Yifan Hong0f529fa2018-01-10 14:51:59 -0800333#pragma clang diagnostic pop
Yifan Honga28729e2018-01-17 13:40:35 -0800334 lft.device.mVendorNdk == rgt.device.mVendorNdk &&
335 lft.device.mSystemSdk == rgt.device.mSystemSdk)) &&
Yifan Hongd4857902017-06-13 14:13:56 -0700336 (lft.mType != SchemaType::FRAMEWORK ||
337 (lft.framework.mKernels == rgt.framework.mKernels &&
338 lft.framework.mSepolicy == rgt.framework.mSepolicy &&
339 lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion));
Yifan Hongfb7469c2017-04-05 19:15:21 -0700340}
341
Yifan Honge7837b12018-10-11 10:38:57 -0700342std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combine(
343 Level deviceLevel, std::vector<Named<CompatibilityMatrix>>* matrices, std::string* error) {
344 // Check type.
345 for (const auto& e : *matrices) {
346 if (e.object.type() != SchemaType::FRAMEWORK) {
347 if (error) {
348 *error = "File \"" + e.name + "\" is not a framework compatibility matrix.";
349 return nullptr;
350 }
351 }
352 }
353
354 // Matrices with unspecified (empty) level are auto-filled with deviceLevel.
Yifan Hongddae77e2017-12-18 16:57:07 -0800355 for (auto& e : *matrices) {
Yifan Hongffcaf992018-01-09 17:08:51 -0800356 if (e.object.level() == Level::UNSPECIFIED) {
Yifan Honge7837b12018-10-11 10:38:57 -0700357 e.object.mLevel = deviceLevel;
Yifan Hongddae77e2017-12-18 16:57:07 -0800358 }
359 }
360
Yifan Honge7837b12018-10-11 10:38:57 -0700361 // Add from low to high FCM version so that optional <kernel> requirements are added correctly.
362 // See comment in addAllAsOptional.
363 std::sort(matrices->begin(), matrices->end(),
364 [](const auto& x, const auto& y) { return x.object.level() < y.object.level(); });
365
366 auto baseMatrix = std::make_unique<CompatibilityMatrix>();
367 baseMatrix->mLevel = deviceLevel;
368 baseMatrix->mType = SchemaType::FRAMEWORK;
369
370 std::vector<std::string> parsedFiles;
371 for (auto& e : *matrices) {
372 if (e.object.level() < deviceLevel) {
373 continue;
Yifan Hongddae77e2017-12-18 16:57:07 -0800374 }
Yifan Honge7837b12018-10-11 10:38:57 -0700375
376 bool success = false;
377 if (e.object.level() == deviceLevel) {
378 success = baseMatrix->addAll(&e, error);
379 } else {
380 success = baseMatrix->addAllAsOptional(&e, error);
381 }
382 if (!success) {
383 if (error) {
384 *error = "Conflict when merging \"" + e.name + "\": " + *error + "\n" +
385 "Previous files:\n" + base::Join(parsedFiles, "\n");
386 }
387 return nullptr;
388 }
389 parsedFiles.push_back(e.name);
Yifan Hongddae77e2017-12-18 16:57:07 -0800390 }
Yifan Honge7837b12018-10-11 10:38:57 -0700391
392 return baseMatrix;
Yifan Hongddae77e2017-12-18 16:57:07 -0800393}
394
Yifan Honge7837b12018-10-11 10:38:57 -0700395bool CompatibilityMatrix::addAll(Named<CompatibilityMatrix>* inputMatrix, std::string* error) {
396 if (!addAllHals(&inputMatrix->object, error) || !addAllXmlFiles(&inputMatrix->object, error) ||
397 !addAllKernels(&inputMatrix->object, error) || !addSepolicy(&inputMatrix->object, error) ||
398 !addAvbMetaVersion(&inputMatrix->object, error)) {
399 if (error) {
400 *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error + ".";
Yifan Hongd6de7f62018-04-26 18:40:02 -0700401 }
Yifan Hongd6de7f62018-04-26 18:40:02 -0700402 }
403 return true;
404}
405
Yifan Honge7837b12018-10-11 10:38:57 -0700406bool CompatibilityMatrix::addAllAsOptional(Named<CompatibilityMatrix>* inputMatrix,
407 std::string* error) {
408 if (!addAllHalsAsOptional(&inputMatrix->object, error) ||
409 !addAllXmlFilesAsOptional(&inputMatrix->object, error) ||
410 !addAllKernelsAsOptional(&inputMatrix->object, error)) {
411 if (error) {
412 *error = "File \"" + inputMatrix->name + "\" cannot be added: " + *error;
Yifan Hongddae77e2017-12-18 16:57:07 -0800413 }
Yifan Honge7837b12018-10-11 10:38:57 -0700414 return false;
Yifan Hongddae77e2017-12-18 16:57:07 -0800415 }
Yifan Honge7837b12018-10-11 10:38:57 -0700416 // ignore <sepolicy> requirement from higher level
417 // ignore <avb> requirement from higher level
418 return true;
Yifan Hongddae77e2017-12-18 16:57:07 -0800419}
420
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800421bool CompatibilityMatrix::forEachInstanceOfVersion(
422 const std::string& package, const Version& expectVersion,
423 const std::function<bool(const MatrixInstance&)>& func) const {
424 for (const MatrixHal* hal : getHals(package)) {
425 bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) {
426 if (matrixInstance.versionRange().contains(expectVersion)) {
427 return func(matrixInstance);
Yifan Honge3a92342018-01-25 17:00:16 -0800428 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800429 return true;
430 });
431 if (!cont) return false;
Yifan Honge3a92342018-01-25 17:00:16 -0800432 }
Yifan Hong2a90ffe2018-03-05 17:45:34 -0800433 return true;
Yifan Honge3a92342018-01-25 17:00:16 -0800434}
435
Yifan Hongdef7e7f2018-03-20 13:27:36 -0700436bool CompatibilityMatrix::matchInstance(const std::string& halName, const Version& version,
437 const std::string& interfaceName,
438 const std::string& instance) const {
439 bool found = false;
440 (void)forEachInstanceOfInterface(halName, version, interfaceName,
441 [&found, &instance](const auto& e) {
442 found |= (e.matchInstance(instance));
443 return !found; // if not found, continue
444 });
445 return found;
446}
447
Yifan Hong52b7fae2018-05-22 16:21:31 -0700448std::string CompatibilityMatrix::getVendorNdkVersion() const {
449 return type() == SchemaType::DEVICE ? device.mVendorNdk.version() : "";
450}
451
Yifan Hong676447a2016-11-15 12:57:23 -0800452} // namespace vintf
453} // namespace android