KernelInfo.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "parse_string.h"
  18. namespace android {
  19. namespace vintf {
  20. const KernelVersion& KernelInfo::version() const {
  21. return mVersion;
  22. }
  23. const std::map<std::string, std::string>& KernelInfo::configs() const {
  24. return mConfigs;
  25. }
  26. bool KernelInfo::matchKernelConfigs(const std::vector<KernelConfig>& matrixConfigs,
  27. std::string* error) const {
  28. for (const KernelConfig& matrixConfig : matrixConfigs) {
  29. const std::string& key = matrixConfig.first;
  30. auto it = this->mConfigs.find(key);
  31. if (it == this->mConfigs.end()) {
  32. // special case: <value type="tristate">n</value> matches if the config doesn't exist.
  33. if (matrixConfig.second == KernelConfigTypedValue::gMissingConfig) {
  34. continue;
  35. }
  36. if (error != nullptr) {
  37. *error = "Missing config " + key;
  38. }
  39. return false;
  40. }
  41. const std::string& kernelValue = it->second;
  42. if (!matrixConfig.second.matchValue(kernelValue)) {
  43. if (error != nullptr) {
  44. *error = "For config " + key + ", value = " + kernelValue + " but required " +
  45. to_string(matrixConfig.second);
  46. }
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. bool KernelInfo::matchKernelVersion(const KernelVersion& minLts) const {
  53. return minLts.version == mVersion.version && minLts.majorRev == mVersion.majorRev &&
  54. minLts.minorRev <= mVersion.minorRev;
  55. }
  56. bool KernelInfo::matchKernelRequirements(const std::vector<MatrixKernel>& kernels,
  57. std::string* error) const {
  58. bool foundMatchedKernelVersion = false;
  59. bool foundMatchedConditions = false;
  60. for (const MatrixKernel& matrixKernel : kernels) {
  61. if (!matchKernelVersion(matrixKernel.minLts())) {
  62. continue;
  63. }
  64. foundMatchedKernelVersion = true;
  65. // ignore this fragment if not all conditions are met.
  66. if (!matchKernelConfigs(matrixKernel.conditions(), error)) {
  67. continue;
  68. }
  69. foundMatchedConditions = true;
  70. if (!matchKernelConfigs(matrixKernel.configs(), error)) {
  71. return false;
  72. }
  73. }
  74. if (!foundMatchedKernelVersion) {
  75. if (error != nullptr) {
  76. std::stringstream ss;
  77. ss << "Framework is incompatible with kernel version " << version()
  78. << ", compatible kernel versions are";
  79. for (const MatrixKernel& matrixKernel : kernels) ss << " " << matrixKernel.minLts();
  80. *error = ss.str();
  81. }
  82. return false;
  83. }
  84. if (!foundMatchedConditions) {
  85. // This should not happen because first <conditions> for each <kernel> must be
  86. // empty. Reject here for inconsistency.
  87. if (error != nullptr) {
  88. error->insert(0, "Framework match kernel version with unmet conditions:");
  89. }
  90. return false;
  91. }
  92. if (error != nullptr) {
  93. error->clear();
  94. }
  95. return true;
  96. }
  97. bool KernelInfo::operator==(const KernelInfo& other) const {
  98. return mVersion == other.mVersion && mConfigs == other.mConfigs;
  99. }
  100. } // namespace vintf
  101. } // namespace android