Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 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 | #include "CompatibilityMatrix.h" |
| 18 | |
Yifan Hong | b6c7f49 | 2018-02-27 14:07:57 -0800 | [diff] [blame] | 19 | #include <iostream> |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 23 | #include <android-base/strings.h> |
| 24 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 25 | #include "parse_string.h" |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 26 | #include "parse_xml.h" |
Yifan Hong | b6c7f49 | 2018-02-27 14:07:57 -0800 | [diff] [blame] | 27 | #include "utils.h" |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 28 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace vintf { |
| 31 | |
Yifan Hong | 4c6962d | 2019-01-30 15:50:46 -0800 | [diff] [blame] | 32 | using details::mergeField; |
| 33 | |
Yifan Hong | 7992f5c | 2020-04-13 15:50:27 -0700 | [diff] [blame] | 34 | bool CompatibilityMatrix::add(MatrixHal&& halToAdd, std::string*) { |
| 35 | CHECK(addInternal(std::move(halToAdd)) != nullptr); |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | bool CompatibilityMatrix::addAllHals(CompatibilityMatrix* other, std::string*) { |
| 40 | std::string internalError; |
| 41 | for (auto& pair : other->mHals) { |
| 42 | CHECK(add(std::move(pair.second), &internalError)) << internalError; |
| 43 | } |
| 44 | other->mHals.clear(); |
| 45 | return true; |
| 46 | } |
| 47 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 48 | bool CompatibilityMatrix::addKernel(MatrixKernel&& kernel, std::string* error) { |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 49 | if (mType != SchemaType::FRAMEWORK) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 50 | if (error) { |
| 51 | *error = "Cannot add <kernel> to a " + to_string(mType) + " compatibility matrix."; |
| 52 | } |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 53 | return false; |
| 54 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 55 | |
Yifan Hong | b343c5b | 2019-12-11 18:13:59 -0800 | [diff] [blame] | 56 | if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) { |
| 57 | kernel.setSourceMatrixLevel(level()); |
| 58 | } |
| 59 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 60 | auto it = framework.mKernels.begin(); |
| 61 | for (; it != framework.mKernels.end(); ++it) { |
Yifan Hong | 38c837b | 2019-12-12 15:22:00 -0800 | [diff] [blame] | 62 | if (it->getSourceMatrixLevel() != kernel.getSourceMatrixLevel()) { |
| 63 | continue; |
| 64 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 65 | if (it->minLts() == kernel.minLts()) { |
| 66 | break; |
| 67 | } |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 68 | if (it->minLts().dropMinor() == kernel.minLts().dropMinor()) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 69 | if (error) { |
Yifan Hong | 38c837b | 2019-12-12 15:22:00 -0800 | [diff] [blame] | 70 | *error = "Kernel version mismatch; for level " + |
| 71 | to_string(kernel.getSourceMatrixLevel()) + ", cannot add " + |
| 72 | to_string(kernel.minLts()) + " because " + to_string(it->minLts()) + |
| 73 | " was added."; |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool seenVersion = it != framework.mKernels.end(); |
| 80 | |
| 81 | if (seenVersion) { |
| 82 | // If no conditions, must be the first among the same minLts |
| 83 | // because O libvintf only checks the first <kernel> tag that version matches. |
| 84 | if (kernel.conditions().empty()) { |
| 85 | // Found first <kernel> with the same minLts. |
| 86 | // Append config if it does not have <condition>s, else error. |
| 87 | if (it->conditions().empty()) { |
| 88 | const auto& configs = kernel.configs(); |
| 89 | it->mConfigs.insert(it->mConfigs.end(), configs.begin(), configs.end()); |
| 90 | } else { |
| 91 | if (error) { |
| 92 | *error = |
| 93 | "Base compatibility matrix has <condition> for the first <kernel> " |
| 94 | "with minlts " + |
| 95 | to_string(kernel.minLts()) + " for unknown reason."; |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | } else { |
| 102 | // First <kernel> of a minLts must not have <condition>'s for backwards compatibility |
| 103 | // with O libvintf. |
| 104 | if (!kernel.conditions().empty()) { |
| 105 | framework.mKernels.push_back(MatrixKernel(KernelVersion{kernel.minLts()}, {})); |
| 106 | } |
| 107 | } |
| 108 | |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 109 | framework.mKernels.push_back(std::move(kernel)); |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
Yifan Hong | 398f4c7 | 2017-04-13 20:18:01 -0700 | [diff] [blame] | 113 | SchemaType CompatibilityMatrix::type() const { |
| 114 | return mType; |
| 115 | } |
| 116 | |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 117 | Level CompatibilityMatrix::level() const { |
| 118 | return mLevel; |
| 119 | } |
| 120 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 121 | status_t CompatibilityMatrix::fetchAllInformation(const FileSystem* fileSystem, |
| 122 | const std::string& path, std::string* error) { |
Yifan Hong | 25e2a77 | 2021-04-16 18:34:28 -0700 | [diff] [blame] | 123 | return details::fetchAllInformation(fileSystem, path, this, error); |
Yifan Hong | 1e5a054 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Yifan Hong | bbfff30 | 2017-06-06 17:10:13 -0700 | [diff] [blame] | 126 | std::string CompatibilityMatrix::getXmlSchemaPath(const std::string& xmlFileName, |
| 127 | const Version& version) const { |
| 128 | using std::literals::string_literals::operator""s; |
| 129 | auto range = getXmlFiles(xmlFileName); |
| 130 | for (auto it = range.first; it != range.second; ++it) { |
| 131 | const MatrixXmlFile& matrixXmlFile = it->second; |
| 132 | if (matrixXmlFile.versionRange().contains(version)) { |
| 133 | if (!matrixXmlFile.overriddenPath().empty()) { |
| 134 | return matrixXmlFile.overriddenPath(); |
| 135 | } |
| 136 | return "/"s + (type() == SchemaType::DEVICE ? "vendor" : "system") + "/etc/" + |
| 137 | xmlFileName + "_V" + std::to_string(matrixXmlFile.versionRange().majorVer) + |
| 138 | "_" + std::to_string(matrixXmlFile.versionRange().maxMinor) + "." + |
| 139 | to_string(matrixXmlFile.format()); |
| 140 | } |
| 141 | } |
| 142 | return ""; |
| 143 | } |
| 144 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 145 | // Split existingHal into a HAL that contains only interface/instance and a HAL |
| 146 | // that does not contain it. Return the HAL that contains only interface/instance. |
| 147 | // - Return nullptr if existingHal does not contain interface/instance |
| 148 | // - Return existingHal if existingHal contains only interface/instance |
| 149 | // - Remove interface/instance from existingHal, and return a new MatrixHal (that is added |
| 150 | // to "this") that contains only interface/instance. |
| 151 | MatrixHal* CompatibilityMatrix::splitInstance(MatrixHal* existingHal, const std::string& interface, |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 152 | const std::string& instanceOrPattern, bool isRegex) { |
| 153 | bool found = false; |
| 154 | bool foundOthers = false; |
| 155 | existingHal->forEachInstance([&](const auto& matrixInstance) { |
| 156 | bool interfaceMatch = matrixInstance.interface() == interface; |
| 157 | bool instanceMatch = false; |
| 158 | if (matrixInstance.isRegex() && isRegex) { |
| 159 | instanceMatch = (matrixInstance.regexPattern() == instanceOrPattern); |
| 160 | } else if (!matrixInstance.isRegex() && !isRegex) { |
| 161 | instanceMatch = (matrixInstance.exactInstance() == instanceOrPattern); |
| 162 | } |
| 163 | |
| 164 | bool match = interfaceMatch && instanceMatch; |
| 165 | |
| 166 | found |= match; |
| 167 | foundOthers |= (!match); |
| 168 | |
| 169 | return !found || !foundOthers; |
| 170 | }); |
| 171 | |
| 172 | if (!found) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 173 | return nullptr; |
| 174 | } |
| 175 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 176 | if (!foundOthers) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 177 | return existingHal; |
| 178 | } |
| 179 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 180 | existingHal->removeInstance(interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 181 | MatrixHal copy = *existingHal; |
| 182 | copy.clearInstances(); |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 183 | copy.insertInstance(interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 184 | |
| 185 | return addInternal(std::move(copy)); |
| 186 | } |
| 187 | |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 188 | // Add all package@other_version::interface/instance as an optional instance. |
| 189 | // If package@this_version::interface/instance is in this (that is, some instance |
| 190 | // with the same package and interface and instance exists), then other_version is |
| 191 | // considered a possible replacement to this_version. |
| 192 | // See LibVintfTest.AddOptionalHal* tests for details. |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 193 | bool CompatibilityMatrix::addAllHalsAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 194 | if (other == nullptr || other->level() <= level()) { |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | for (auto& pair : other->mHals) { |
| 199 | const std::string& name = pair.first; |
| 200 | MatrixHal& halToAdd = pair.second; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 201 | |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 202 | std::set<std::pair<std::string, std::string>> insertedInstances; |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 203 | std::set<std::pair<std::string, std::string>> insertedRegex; |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 204 | auto existingHals = getHals(name); |
| 205 | |
| 206 | halToAdd.forEachInstance([&](const std::vector<VersionRange>& versionRanges, |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 207 | const std::string& interface, |
| 208 | const std::string& instanceOrPattern, bool isRegex) { |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 209 | for (auto* existingHal : existingHals) { |
Yifan Hong | 5bb4fbf | 2019-09-10 14:47:28 -0700 | [diff] [blame] | 210 | // Ignore HALs with different format. |
| 211 | if (halToAdd.format != existingHal->format) { |
| 212 | continue; |
| 213 | } |
| 214 | |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 215 | MatrixHal* splitInstance = |
| 216 | this->splitInstance(existingHal, interface, instanceOrPattern, isRegex); |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 217 | if (splitInstance != nullptr) { |
| 218 | splitInstance->insertVersionRanges(versionRanges); |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 219 | if (isRegex) { |
| 220 | insertedRegex.insert(std::make_pair(interface, instanceOrPattern)); |
| 221 | } else { |
| 222 | insertedInstances.insert(std::make_pair(interface, instanceOrPattern)); |
| 223 | } |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 224 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 225 | } |
Yifan Hong | e7e4553 | 2018-03-16 18:11:49 -0700 | [diff] [blame] | 226 | return true; |
| 227 | }); |
| 228 | |
| 229 | // Add the remaining instances. |
| 230 | for (const auto& pair : insertedInstances) { |
Yifan Hong | 643a9ef | 2018-03-21 14:13:55 -0700 | [diff] [blame] | 231 | halToAdd.removeInstance(pair.first, pair.second, false /* isRegex */); |
| 232 | } |
| 233 | for (const auto& pair : insertedRegex) { |
| 234 | halToAdd.removeInstance(pair.first, pair.second, true /* isRegex */); |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 235 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 236 | |
Yifan Hong | 7e9e04d | 2018-03-20 13:06:00 -0700 | [diff] [blame] | 237 | if (halToAdd.instancesCount() > 0) { |
Yifan Hong | 7967d7b | 2018-03-15 17:08:58 -0700 | [diff] [blame] | 238 | halToAdd.setOptional(true); |
| 239 | if (!add(std::move(halToAdd))) { |
| 240 | if (error) { |
| 241 | *error = "Cannot add HAL " + name + " for unknown reason."; |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 242 | } |
| 243 | return false; |
| 244 | } |
Yifan Hong | dbe9db3 | 2017-12-11 19:06:11 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | return true; |
| 248 | } |
| 249 | |
Yifan Hong | d4b92fe | 2017-12-20 15:29:03 -0800 | [diff] [blame] | 250 | bool CompatibilityMatrix::addAllXmlFilesAsOptional(CompatibilityMatrix* other, std::string* error) { |
| 251 | if (other == nullptr || other->level() <= level()) { |
| 252 | return true; |
| 253 | } |
| 254 | for (auto& pair : other->mXmlFiles) { |
| 255 | const std::string& name = pair.first; |
| 256 | MatrixXmlFile& xmlFileToAdd = pair.second; |
| 257 | |
| 258 | xmlFileToAdd.mOptional = true; |
| 259 | if (!addXmlFile(std::move(xmlFileToAdd))) { |
| 260 | if (error) { |
| 261 | *error = "Cannot add XML File " + name + " for unknown reason."; |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | return true; |
| 267 | } |
| 268 | |
Yifan Hong | 38c837b | 2019-12-12 15:22:00 -0800 | [diff] [blame] | 269 | // Merge Kernel. See KernelInfo::getMatchedKernelRequirements for details on compatibility checks. |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 270 | bool CompatibilityMatrix::addAllKernels(CompatibilityMatrix* other, std::string* error) { |
| 271 | for (MatrixKernel& kernel : other->framework.mKernels) { |
Yifan Hong | a39ecfe | 2019-12-11 17:55:30 -0800 | [diff] [blame] | 272 | if (kernel.getSourceMatrixLevel() == Level::UNSPECIFIED) { |
| 273 | kernel.setSourceMatrixLevel(other->level()); |
| 274 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 275 | KernelVersion ver = kernel.minLts(); |
| 276 | if (!addKernel(std::move(kernel), error)) { |
| 277 | if (error) { |
| 278 | *error = "Cannot add kernel version " + to_string(ver) + ": " + *error; |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | return true; |
| 284 | } |
| 285 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 286 | bool CompatibilityMatrix::addSepolicy(CompatibilityMatrix* other, std::string* error) { |
| 287 | bool success = mergeField(&this->framework.mSepolicy, &other->framework.mSepolicy); |
| 288 | if (!success && error) *error = "<sepolicy> is already defined"; |
| 289 | return success; |
| 290 | } |
| 291 | |
| 292 | bool CompatibilityMatrix::addAvbMetaVersion(CompatibilityMatrix* other, std::string* error) { |
| 293 | bool success = mergeField(&this->framework.mAvbMetaVersion, &other->framework.mAvbMetaVersion); |
| 294 | if (!success && error) *error = "<avb><vbmeta-version> is already defined"; |
| 295 | return success; |
| 296 | } |
| 297 | |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 298 | bool CompatibilityMatrix::addVndk(CompatibilityMatrix* other, std::string* error) { |
| 299 | #pragma clang diagnostic push |
| 300 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 301 | bool success = mergeField(&this->device.mVndk, &other->device.mVndk); |
| 302 | #pragma clang diagnostic pop |
| 303 | if (!success && error) *error = "<vndk> is already defined"; |
| 304 | return success; |
| 305 | } |
| 306 | |
| 307 | bool CompatibilityMatrix::addVendorNdk(CompatibilityMatrix* other, std::string* error) { |
| 308 | bool success = mergeField(&this->device.mVendorNdk, &other->device.mVendorNdk); |
| 309 | if (!success && error) *error = "<vendor-ndk> is already defined"; |
| 310 | return success; |
| 311 | } |
| 312 | |
| 313 | bool CompatibilityMatrix::addSystemSdk(CompatibilityMatrix* other, std::string* /* error */) { |
| 314 | this->device.mSystemSdk.addAll(&other->device.mSystemSdk); |
| 315 | return true; |
| 316 | } |
| 317 | |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 318 | bool operator==(const CompatibilityMatrix &lft, const CompatibilityMatrix &rgt) { |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 319 | // ignore fileName(). |
Yifan Hong | 2027a49 | 2017-12-11 15:21:19 -0800 | [diff] [blame] | 320 | return lft.mType == rgt.mType && lft.mLevel == rgt.mLevel && lft.mHals == rgt.mHals && |
| 321 | lft.mXmlFiles == rgt.mXmlFiles && |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 322 | (lft.mType != SchemaType::DEVICE || |
| 323 | ( |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 324 | #pragma clang diagnostic push |
| 325 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Yifan Hong | feb454e | 2018-01-09 16:16:40 -0800 | [diff] [blame] | 326 | lft.device.mVndk == rgt.device.mVndk && |
Yifan Hong | 0f529fa | 2018-01-10 14:51:59 -0800 | [diff] [blame] | 327 | #pragma clang diagnostic pop |
Yifan Hong | a28729e | 2018-01-17 13:40:35 -0800 | [diff] [blame] | 328 | lft.device.mVendorNdk == rgt.device.mVendorNdk && |
| 329 | lft.device.mSystemSdk == rgt.device.mSystemSdk)) && |
Yifan Hong | d485790 | 2017-06-13 14:13:56 -0700 | [diff] [blame] | 330 | (lft.mType != SchemaType::FRAMEWORK || |
| 331 | (lft.framework.mKernels == rgt.framework.mKernels && |
| 332 | lft.framework.mSepolicy == rgt.framework.mSepolicy && |
| 333 | lft.framework.mAvbMetaVersion == rgt.framework.mAvbMetaVersion)); |
Yifan Hong | fb7469c | 2017-04-05 19:15:21 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 336 | std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combine( |
Yifan Hong | 507815b | 2021-05-21 16:48:37 -0700 | [diff] [blame] | 337 | Level deviceLevel, Level kernelLevel, std::vector<CompatibilityMatrix>* matrices, |
| 338 | std::string* error) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 339 | // Check type. |
| 340 | for (const auto& e : *matrices) { |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 341 | if (e.type() != SchemaType::FRAMEWORK) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 342 | if (error) { |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 343 | *error = "File \"" + e.fileName() + "\" is not a framework compatibility matrix."; |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 344 | return nullptr; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | // Matrices with unspecified (empty) level are auto-filled with deviceLevel. |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 350 | for (auto& e : *matrices) { |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 351 | if (e.level() == Level::UNSPECIFIED) { |
| 352 | e.mLevel = deviceLevel; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 356 | // Add from low to high FCM version so that optional <kernel> requirements are added correctly. |
| 357 | // See comment in addAllAsOptional. |
| 358 | std::sort(matrices->begin(), matrices->end(), |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 359 | [](const auto& x, const auto& y) { return x.level() < y.level(); }); |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 360 | |
| 361 | auto baseMatrix = std::make_unique<CompatibilityMatrix>(); |
| 362 | baseMatrix->mLevel = deviceLevel; |
| 363 | baseMatrix->mType = SchemaType::FRAMEWORK; |
| 364 | |
| 365 | std::vector<std::string> parsedFiles; |
| 366 | for (auto& e : *matrices) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 367 | bool success = false; |
Yifan Hong | 507815b | 2021-05-21 16:48:37 -0700 | [diff] [blame] | 368 | if (e.level() < deviceLevel) { |
| 369 | if (kernelLevel == Level::UNSPECIFIED) continue; |
| 370 | if (e.level() < kernelLevel) continue; |
| 371 | success = baseMatrix->addAllKernels(&e, error); |
| 372 | } else if (e.level() == deviceLevel) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 373 | success = baseMatrix->addAll(&e, error); |
| 374 | } else { |
| 375 | success = baseMatrix->addAllAsOptional(&e, error); |
| 376 | } |
| 377 | if (!success) { |
| 378 | if (error) { |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 379 | *error = "Conflict when merging \"" + e.fileName() + "\": " + *error + "\n" + |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 380 | "Previous files:\n" + base::Join(parsedFiles, "\n"); |
| 381 | } |
| 382 | return nullptr; |
| 383 | } |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 384 | parsedFiles.push_back(e.fileName()); |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 385 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 386 | |
| 387 | return baseMatrix; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 390 | std::unique_ptr<CompatibilityMatrix> CompatibilityMatrix::combineDeviceMatrices( |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 391 | std::vector<CompatibilityMatrix>* matrices, std::string* error) { |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 392 | auto baseMatrix = std::make_unique<CompatibilityMatrix>(); |
| 393 | baseMatrix->mType = SchemaType::DEVICE; |
| 394 | |
| 395 | std::vector<std::string> parsedFiles; |
| 396 | for (auto& e : *matrices) { |
| 397 | bool success = baseMatrix->addAll(&e, error); |
| 398 | if (!success) { |
| 399 | if (error) { |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 400 | *error = "Conflict when merging \"" + e.fileName() + "\": " + *error + "\n" + |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 401 | "Previous files:\n" + base::Join(parsedFiles, "\n"); |
| 402 | } |
| 403 | return nullptr; |
| 404 | } |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 405 | parsedFiles.push_back(e.fileName()); |
Yifan Hong | 5a51ea5 | 2019-04-22 14:54:57 -0700 | [diff] [blame] | 406 | } |
| 407 | return baseMatrix; |
| 408 | } |
| 409 | |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 410 | bool CompatibilityMatrix::addAll(CompatibilityMatrix* inputMatrix, std::string* error) { |
| 411 | if (!addAllHals(inputMatrix, error) || !addAllXmlFiles(inputMatrix, error) || |
| 412 | !addAllKernels(inputMatrix, error) || !addSepolicy(inputMatrix, error) || |
| 413 | !addAvbMetaVersion(inputMatrix, error) || !addVndk(inputMatrix, error) || |
| 414 | !addVendorNdk(inputMatrix, error) || !addSystemSdk(inputMatrix, error)) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 415 | if (error) { |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 416 | *error = "File \"" + inputMatrix->fileName() + "\" cannot be added: " + *error + "."; |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 417 | } |
Yifan Hong | 5390e3b | 2019-01-31 14:13:00 -0800 | [diff] [blame] | 418 | return false; |
Yifan Hong | d6de7f6 | 2018-04-26 18:40:02 -0700 | [diff] [blame] | 419 | } |
| 420 | return true; |
| 421 | } |
| 422 | |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 423 | bool CompatibilityMatrix::addAllAsOptional(CompatibilityMatrix* inputMatrix, std::string* error) { |
| 424 | if (!addAllHalsAsOptional(inputMatrix, error) || |
| 425 | !addAllXmlFilesAsOptional(inputMatrix, error) || !addAllKernels(inputMatrix, error)) { |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 426 | if (error) { |
Yifan Hong | a83d0e4 | 2020-04-13 13:07:31 -0700 | [diff] [blame] | 427 | *error = "File \"" + inputMatrix->fileName() + "\" cannot be added: " + *error; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 428 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 429 | return false; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 430 | } |
Yifan Hong | e7837b1 | 2018-10-11 10:38:57 -0700 | [diff] [blame] | 431 | // ignore <sepolicy> requirement from higher level |
| 432 | // ignore <avb> requirement from higher level |
| 433 | return true; |
Yifan Hong | ddae77e | 2017-12-18 16:57:07 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 436 | bool CompatibilityMatrix::forEachInstanceOfVersion( |
Yifan Hong | fc7ad27 | 2019-09-10 19:49:15 -0700 | [diff] [blame] | 437 | HalFormat format, const std::string& package, const Version& expectVersion, |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 438 | const std::function<bool(const MatrixInstance&)>& func) const { |
| 439 | for (const MatrixHal* hal : getHals(package)) { |
| 440 | bool cont = hal->forEachInstance([&](const MatrixInstance& matrixInstance) { |
Yifan Hong | fc7ad27 | 2019-09-10 19:49:15 -0700 | [diff] [blame] | 441 | if (matrixInstance.format() == format && |
| 442 | matrixInstance.versionRange().contains(expectVersion)) { |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 443 | return func(matrixInstance); |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 444 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 445 | return true; |
| 446 | }); |
| 447 | if (!cont) return false; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 448 | } |
Yifan Hong | 2a90ffe | 2018-03-05 17:45:34 -0800 | [diff] [blame] | 449 | return true; |
Yifan Hong | e3a9234 | 2018-01-25 17:00:16 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Yifan Hong | 1dc5a47 | 2019-09-10 19:40:06 -0700 | [diff] [blame] | 452 | bool CompatibilityMatrix::matchInstance(HalFormat format, const std::string& halName, |
| 453 | const Version& version, const std::string& interfaceName, |
Yifan Hong | def7e7f | 2018-03-20 13:27:36 -0700 | [diff] [blame] | 454 | const std::string& instance) const { |
| 455 | bool found = false; |
Yifan Hong | 1dc5a47 | 2019-09-10 19:40:06 -0700 | [diff] [blame] | 456 | (void)forEachInstanceOfInterface(format, halName, version, interfaceName, |
Yifan Hong | def7e7f | 2018-03-20 13:27:36 -0700 | [diff] [blame] | 457 | [&found, &instance](const auto& e) { |
| 458 | found |= (e.matchInstance(instance)); |
| 459 | return !found; // if not found, continue |
| 460 | }); |
| 461 | return found; |
| 462 | } |
| 463 | |
Inseob Kim | 23873a4 | 2021-04-29 12:59:19 +0900 | [diff] [blame] | 464 | std::vector<VersionRange> CompatibilityMatrix::getSepolicyVersions() const { |
| 465 | if (type() == SchemaType::FRAMEWORK) return framework.mSepolicy.sepolicyVersions(); |
| 466 | return {}; |
| 467 | } |
| 468 | |
Yifan Hong | 52b7fae | 2018-05-22 16:21:31 -0700 | [diff] [blame] | 469 | std::string CompatibilityMatrix::getVendorNdkVersion() const { |
| 470 | return type() == SchemaType::DEVICE ? device.mVendorNdk.version() : ""; |
| 471 | } |
| 472 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 473 | Level CompatibilityMatrix::getSourceMatrixLevel(const MatrixKernel* matrixKernel) const { |
| 474 | CHECK(std::find_if(framework.mKernels.begin(), framework.mKernels.end(), |
| 475 | [matrixKernel](const auto& e) { return &e == matrixKernel; }) != |
| 476 | framework.mKernels.end()); |
| 477 | Level ret = matrixKernel->getSourceMatrixLevel(); |
| 478 | if (ret != Level::UNSPECIFIED) return ret; |
| 479 | return level(); |
| 480 | } |
| 481 | |
Yifan Hong | 676447a | 2016-11-15 12:57:23 -0800 | [diff] [blame] | 482 | } // namespace vintf |
| 483 | } // namespace android |