Yifan Hong | ccf967b | 2017-01-18 11:04:19 -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 | |
| 18 | #define LOG_TAG "libvintf" |
| 19 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 20 | #include "RuntimeInfo.h" |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 21 | |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 22 | #include "CompatibilityMatrix.h" |
| 23 | #include "parse_string.h" |
| 24 | |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace vintf { |
| 27 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 28 | const std::string &RuntimeInfo::osName() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 29 | return mOsName; |
| 30 | } |
| 31 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 32 | const std::string &RuntimeInfo::nodeName() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 33 | return mNodeName; |
| 34 | } |
| 35 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 36 | const std::string &RuntimeInfo::osRelease() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 37 | return mOsRelease; |
| 38 | } |
| 39 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 40 | const std::string &RuntimeInfo::osVersion() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 41 | return mOsVersion; |
| 42 | } |
| 43 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 44 | const std::string &RuntimeInfo::hardwareId() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 45 | return mHardwareId; |
| 46 | } |
| 47 | |
Yifan Hong | fa2b18b | 2017-04-12 19:40:00 -0700 | [diff] [blame^] | 48 | const std::vector<std::string> &RuntimeInfo::sepolicyFilePaths() const { |
| 49 | return mSepolicyFilePaths; |
| 50 | } |
| 51 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 52 | size_t RuntimeInfo::kernelSepolicyVersion() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 53 | return mKernelSepolicyVersion; |
| 54 | } |
| 55 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 56 | bool RuntimeInfo::checkCompatibility(const CompatibilityMatrix &mat, |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 57 | std::string *error) const { |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 58 | if (mat.mType != SchemaType::FRAMEWORK) { |
| 59 | if (error != nullptr) { |
| 60 | *error = "Should not check runtime info against " + to_string(mat.mType) |
| 61 | + " compatibility matrix."; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | if (kernelSepolicyVersion() != mat.framework.mSepolicy.kernelSepolicyVersion()) { |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 66 | if (error != nullptr) { |
| 67 | *error = "kernelSepolicyVersion = " + to_string(kernelSepolicyVersion()) |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame] | 68 | + " but required " + to_string(mat.framework.mSepolicy.kernelSepolicyVersion()); |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Yifan Hong | e8b7d95 | 2017-04-04 15:44:26 -0700 | [diff] [blame] | 73 | // TODO(b/35217573): check sepolicy version against mat.mSepolicy.sepolicyVersion() here. |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 74 | |
| 75 | const MatrixKernel *matrixKernel = mat.findKernel(this->mKernelVersion); |
| 76 | if (matrixKernel == nullptr) { |
| 77 | if (error != nullptr) { |
| 78 | *error = "Cannot find suitable kernel entry for " + to_string(mKernelVersion); |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | for (const KernelConfig &matrixConfig : matrixKernel->configs()) { |
| 83 | const std::string &key = matrixConfig.first; |
Yifan Hong | f1af752 | 2017-02-16 18:00:55 -0800 | [diff] [blame] | 84 | auto it = this->mKernelConfigs.find(key); |
| 85 | if (it == this->mKernelConfigs.end()) { |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 86 | // special case: <value type="tristate">n</value> matches if the config doesn't exist. |
| 87 | if (matrixConfig.second == KernelConfigTypedValue::gMissingConfig) { |
| 88 | continue; |
| 89 | } |
| 90 | if (error != nullptr) { |
| 91 | *error = "Missing config " + key; |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | const std::string &kernelValue = it->second; |
| 96 | if (!matrixConfig.second.matchValue(kernelValue)) { |
| 97 | if (error != nullptr) { |
| 98 | *error = "For config " + key + ", value = " + kernelValue |
| 99 | + " but required " + to_string(matrixConfig.second); |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 107 | } // namespace vintf |
| 108 | } // namespace android |