Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include "KernelInfo.h" |
| 17 | |
| 18 | #include "parse_string.h" |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 19 | #include "parse_xml.h" |
Yifan Hong | f66094f | 2019-12-11 14:23:08 -0800 | [diff] [blame] | 20 | #include "utils.h" |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace vintf { |
| 24 | |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 25 | extern XmlConverter<KernelInfo>& gKernelInfoConverter; |
| 26 | |
Yifan Hong | f66094f | 2019-12-11 14:23:08 -0800 | [diff] [blame] | 27 | using details::mergeField; |
| 28 | |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 29 | const KernelVersion& KernelInfo::version() const { |
| 30 | return mVersion; |
| 31 | } |
| 32 | |
| 33 | const std::map<std::string, std::string>& KernelInfo::configs() const { |
| 34 | return mConfigs; |
| 35 | } |
| 36 | |
Yifan Hong | e624844 | 2019-12-11 13:33:00 -0800 | [diff] [blame] | 37 | Level KernelInfo::level() const { |
| 38 | return mLevel; |
| 39 | } |
| 40 | |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 41 | bool KernelInfo::matchKernelConfigs(const std::vector<KernelConfig>& matrixConfigs, |
| 42 | std::string* error) const { |
| 43 | for (const KernelConfig& matrixConfig : matrixConfigs) { |
| 44 | const std::string& key = matrixConfig.first; |
| 45 | auto it = this->mConfigs.find(key); |
| 46 | if (it == this->mConfigs.end()) { |
| 47 | // special case: <value type="tristate">n</value> matches if the config doesn't exist. |
| 48 | if (matrixConfig.second == KernelConfigTypedValue::gMissingConfig) { |
| 49 | continue; |
| 50 | } |
| 51 | if (error != nullptr) { |
| 52 | *error = "Missing config " + key; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | const std::string& kernelValue = it->second; |
| 57 | if (!matrixConfig.second.matchValue(kernelValue)) { |
| 58 | if (error != nullptr) { |
| 59 | *error = "For config " + key + ", value = " + kernelValue + " but required " + |
| 60 | to_string(matrixConfig.second); |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool KernelInfo::matchKernelVersion(const KernelVersion& minLts) const { |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 69 | return mVersion.dropMinor() == minLts.dropMinor() && minLts.minorRev <= mVersion.minorRev; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 72 | std::vector<const MatrixKernel*> KernelInfo::getMatchedKernelRequirements( |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 73 | const std::vector<MatrixKernel>& kernels, Level kernelLevel, std::string* error) const { |
| 74 | std::map<Level, std::vector<const MatrixKernel*>> kernelsForLevel; |
| 75 | for (const MatrixKernel& matrixKernel : kernels) { |
| 76 | const auto& minLts = matrixKernel.minLts(); |
| 77 | auto matrixKernelLevel = matrixKernel.getSourceMatrixLevel(); |
| 78 | |
| 79 | // Filter out kernels with different x.y. |
| 80 | if (mVersion.dropMinor() != minLts.dropMinor()) { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | // Check matrix kernel level |
| 85 | |
| 86 | // Use legacy behavior when kernel FCM version is not specified. Blindly add all of them |
| 87 | // here. The correct one (with smallest matrixKernelLevel) will be picked later. |
| 88 | if (kernelLevel == Level::UNSPECIFIED) { |
| 89 | kernelsForLevel[matrixKernelLevel].push_back(&matrixKernel); |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | if (matrixKernelLevel == Level::UNSPECIFIED) { |
| 94 | if (error) { |
| 95 | *error = "Seen unspecified source matrix level; this should not happen."; |
| 96 | } |
| 97 | return {}; |
| 98 | } |
| 99 | |
| 100 | if (matrixKernelLevel < kernelLevel) { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | // matrix level >= kernel level |
| 105 | kernelsForLevel[matrixKernelLevel].push_back(&matrixKernel); |
| 106 | } |
| 107 | |
| 108 | if (kernelsForLevel.empty()) { |
| 109 | if (error) { |
| 110 | *error = "No kernel entry found for kernel version " + to_string(mVersion.dropMinor()); |
| 111 | } |
| 112 | return {}; |
| 113 | } |
| 114 | |
| 115 | // At this point, kernelsForLevel contains kernel requirements for each level. |
| 116 | // For example, if the running kernel version is 4.14.y then kernelsForLevel contains |
| 117 | // 4.14-p, 4.14-q, 4.14-r. |
| 118 | // (This excludes kernels < kernel FCM version, or device FCM version if kernel FCM version is |
| 119 | // empty. For example, if device level = Q and kernel level is unspecified, this list only |
| 120 | // contains 4.14-q and 4.14-r). |
| 121 | |
| 122 | // Use legacy behavior when kernel FCM version is not specified. e.g. target FCM version 3 (P) |
| 123 | // matches kernel 4.4-p, 4.9-p, 4.14-p, 4.19-q, etc., but not 4.9-q or 4.14-q. |
| 124 | // Since we already filtered |kernels| based on kernel version, we only need to check the first |
| 125 | // item in kernelsForLevel. |
| 126 | // Note that this excludes *-r and above kernels. Devices with target FCM version >= 5 (R) must |
| 127 | // state kernel FCM version explicitly in the device manifest. The value is automatically |
| 128 | // inserted for devices with target FCM version >= 5 when manifest is built with assemble_vintf. |
| 129 | if (kernelLevel == Level::UNSPECIFIED) { |
| 130 | auto [matrixKernelLevel, matrixKernels] = *kernelsForLevel.begin(); |
| 131 | |
| 132 | // Do not allow *-r and above kernels. |
| 133 | if (matrixKernelLevel != Level::UNSPECIFIED && matrixKernelLevel >= Level::R) { |
| 134 | if (error) { |
| 135 | KernelInfo msg; |
| 136 | msg.mLevel = Level::R; |
| 137 | *error = "Kernel FCM version is not specified, but kernel version " + |
| 138 | to_string(mVersion) + |
| 139 | " is found. Fix by specifying kernel FCM version in device manifest." |
| 140 | "For example, for a *-r kernel:\n" + |
| 141 | gKernelInfoConverter(msg); |
| 142 | } |
| 143 | return {}; |
| 144 | } |
| 145 | |
| 146 | auto matchedMatrixKernels = getMatchedKernelVersionAndConfigs(matrixKernels, error); |
| 147 | if (matchedMatrixKernels.empty()) { |
| 148 | return {}; |
| 149 | } |
| 150 | return matchedMatrixKernels; |
| 151 | } |
| 152 | |
| 153 | // Use new behavior when kernel FCM version is specified. e.g. kernel FCM version 3 (P) |
| 154 | // matches kernel 4.4-p, 4.9-p, 4.14-p, 4.9-q, 4.14-q, 4.14-r etc., but not 5.4-r. |
| 155 | // Note we already filtered |kernels| based on kernel version. |
| 156 | auto [firstMatrixKernelLevel, firstMatrixKernels] = *kernelsForLevel.begin(); |
| 157 | if (firstMatrixKernelLevel == Level::UNSPECIFIED || firstMatrixKernelLevel > kernelLevel) { |
| 158 | if (error) { |
| 159 | *error = "Kernel FCM Version is " + to_string(kernelLevel) + " and kernel version is " + |
| 160 | to_string(mVersion) + |
| 161 | ", but the first kernel FCM version allowed for kernel version " + |
| 162 | to_string(mVersion.dropMinor()) + ".y is " + to_string(firstMatrixKernelLevel); |
| 163 | } |
| 164 | return {}; |
| 165 | } |
| 166 | for (auto [matrixKernelLevel, matrixKernels] : kernelsForLevel) { |
| 167 | if (matrixKernelLevel == Level::UNSPECIFIED || matrixKernelLevel < kernelLevel) { |
| 168 | continue; |
| 169 | } |
| 170 | std::string errorForLevel; |
| 171 | auto matchedMatrixKernels = |
| 172 | getMatchedKernelVersionAndConfigs(matrixKernels, &errorForLevel); |
| 173 | if (matchedMatrixKernels.empty()) { |
| 174 | if (error) { |
| 175 | *error += "For kernel requirements at matrix level " + |
| 176 | to_string(matrixKernelLevel) + ", " + errorForLevel + "\n"; |
| 177 | } |
| 178 | continue; |
| 179 | } |
| 180 | return matchedMatrixKernels; |
| 181 | } |
| 182 | |
| 183 | if (error) { |
| 184 | error->insert(0, "No compatible kernel requirement found (kernel FCM version = " + |
| 185 | to_string(kernelLevel) + ").\n"); |
| 186 | } |
| 187 | return {}; |
| 188 | } |
| 189 | |
| 190 | std::vector<const MatrixKernel*> KernelInfo::getMatchedKernelVersionAndConfigs( |
| 191 | const std::vector<const MatrixKernel*>& kernels, std::string* error) const { |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 192 | std::vector<const MatrixKernel*> result; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 193 | bool foundMatchedKernelVersion = false; |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 194 | for (const MatrixKernel* matrixKernel : kernels) { |
| 195 | if (!matchKernelVersion(matrixKernel->minLts())) { |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 196 | continue; |
| 197 | } |
| 198 | foundMatchedKernelVersion = true; |
| 199 | // ignore this fragment if not all conditions are met. |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 200 | if (!matchKernelConfigs(matrixKernel->conditions(), error)) { |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 201 | continue; |
| 202 | } |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 203 | if (!matchKernelConfigs(matrixKernel->configs(), error)) { |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 204 | return {}; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 205 | } |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 206 | result.push_back(matrixKernel); |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 207 | } |
| 208 | if (!foundMatchedKernelVersion) { |
| 209 | if (error != nullptr) { |
| 210 | std::stringstream ss; |
| 211 | ss << "Framework is incompatible with kernel version " << version() |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 212 | << ", compatible kernel versions are:"; |
| 213 | for (const MatrixKernel* matrixKernel : kernels) { |
| 214 | ss << "\n Minimum LTS: " << matrixKernel->minLts() |
| 215 | << ", kernel FCM version: " << matrixKernel->getSourceMatrixLevel() |
| 216 | << (matrixKernel->conditions().empty() ? "\n" : ", with conditionals"); |
| 217 | }; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 218 | *error = ss.str(); |
| 219 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 220 | return {}; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 221 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 222 | if (result.empty()) { |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 223 | // This means matchKernelVersion passes but all matchKernelConfigs(conditions) fails. |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 224 | // This should not happen because first <conditions> for each <kernel> must be |
| 225 | // empty. Reject here for inconsistency. |
| 226 | if (error != nullptr) { |
Yifan Hong | 89093ed | 2019-12-12 12:16:01 -0800 | [diff] [blame] | 227 | error->insert(0, "Framework matches kernel version with unmet conditions."); |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 228 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 229 | return {}; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 230 | } |
| 231 | if (error != nullptr) { |
| 232 | error->clear(); |
| 233 | } |
Yifan Hong | 1e8febd | 2019-08-07 16:17:19 -0700 | [diff] [blame] | 234 | return result; |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Yifan Hong | a45f77d | 2018-12-03 16:42:52 -0800 | [diff] [blame] | 237 | bool KernelInfo::operator==(const KernelInfo& other) const { |
| 238 | return mVersion == other.mVersion && mConfigs == other.mConfigs; |
| 239 | } |
| 240 | |
Yifan Hong | f66094f | 2019-12-11 14:23:08 -0800 | [diff] [blame] | 241 | bool KernelInfo::merge(KernelInfo* other, std::string* error) { |
| 242 | if (!mergeField(&mVersion, &other->mVersion)) { |
| 243 | if (error) { |
| 244 | *error = "Conflicting kernel version: " + to_string(version()) + " vs. " + |
| 245 | to_string(other->version()); |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | // Do not allow merging configs. One of them must be empty. |
| 251 | if (!mergeField(&mConfigs, &other->mConfigs)) { |
| 252 | if (error) { |
| 253 | *error = "Found <kernel><config> items in two manifests."; |
| 254 | } |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | if (!mergeField(&mLevel, &other->mLevel, Level::UNSPECIFIED)) { |
| 259 | if (error) { |
| 260 | *error = "Conflicting kernel level: " + to_string(level()) + " vs. " + |
| 261 | to_string(other->level()); |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | return true; |
| 266 | } |
| 267 | |
Yifan Hong | 90ed997 | 2018-12-03 15:08:34 -0800 | [diff] [blame] | 268 | } // namespace vintf |
| 269 | } // namespace android |